Linux crt1 o undefined reference to main

C++ Error: undefined reference to `main’

Solution: This error I know from cases when I forgot to add the object file for the main program (the one that starts with 😉 ). Solution 2: Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs.

Crt1.o: In function `_start’: — undefined reference to `main’ in Linux

Try adding -nostartfiles to your linker options, i.e.

From the gcc documentation:

-nostartfiles Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used. 

This causes crt1.o not to be linked (it’s normally linked by default) — normally only used when you implement your own _start code.

-shared link option must be used when you compile a .so

The issue for me was, I by mistake put int main() in a namespace. Make sure don’t do that otherwise you will get this annoying link error.

Undefined reference when calling inline function, According to the manual, passing -std=gnu11 enables C99 instead of GNU inline semantics. This means inline, static inline and extern inline all behave differently. In particular, inline expects an external definition in a separate translation unit (which you can provide without duplicating the definition — see this answer ).

C++ Error: undefined reference to `main’

You should be able to compile list.cpp , you can’t link it unless you have a main program. (That might be a slight oversimplification.)

The way to compile a source file without linking it depends on what compiler you’re using. If you’re using g++ , the command would be:

That will generate an object file containing the machine code for your class. Depending on your compiler and OS, it might be called list.o or list.obj .

it will assume that you’ve defined a main function and try to generate an executable, resulting in the error you’ve seen (because you haven’t defined a main function).

Читайте также:  Install gcc linux apt get

At some point, of course, you’ll need a program that uses your class. To do that, you’ll need another .cpp source file that has a #include «list.h» and a main() function. You can compile that source file and link the resulting object together with the object generated from list.cpp to generate a working executable. With g++ , you can do that in one step, for example:

You have to have a main function somewhere. It doesn’t necessarily have to be in list.cpp . And as a matter of style and code organization, it probably shouldn’t be in list.cpp ; you might want to be able to use that class from more than one main program.

Undefined reference to main() means that your program lacks a main() function, which is mandatory for all C++ programs. Add this somewhere:

Makefile error: undefined reference to main, When you compile without the -c flag, gcc tries to link the program. Since the main function is most probably in the main.c and not in db.c, the linker fails when searching for the main function in db.c. This means that you need to tell the compiler that you just want output that is not yet linked, but translated into an …

In function `_start’: (.text+0x20): undefined reference to `main’ collect2: ld returned 1 exit status

To make a static library, use ar :

ar rcs mylibrary.a a.o b.o c.o 

To make a shared library, use gcc to link:

gcc -o mylibrary.so -shared a.o b.o. c.o 

If you are trying to build a library, the options depend on whether you want to build a static or a shared library. For example,

# shared library g++ -shared -o name.so obj1.o obj2.o obj3.o 

A static library is essentially an archive, which you can build from the .o files using the ar command.

ar mylib.a obj1.o obj2.o obj3.o 

If you are trying to compile an object file, you need to pass the -c option:

Otherwise, g++ attempts to compile an executable, and this requires a main function.

Undefined reference to main — collect2: ld returned 1 exit, It means that es3.c does not define a main function, and you are attempting to create an executable out of it. An executable needs to have an entry point, thereby the linker complains. To compile only to an object file, use the -c option: gcc es3.c -c gcc es3.o main.c -o es3. The above compiles es3.c to an object file, then …

Читайте также:  Auto restart service linux

FORTRAN: undefined reference to main in crt1.o in function ‘_start’

This error I know from cases when I forgot to add the object file for the main program (the one that starts with program foo 😉 ). Could it be that you miss that one as well?

C — ld complains: in function «_start», undefined reference, This causes the references to external functions to be scrapped: when final binary is executed, neither the menu options nor the buttons react. If I do final linking step with gcc the binary works perfectly (even though it was really compiled with clang and gcc only does the linking). Since I’m trying to completely …

Источник

c++ linux compile error: undefined reference to `main’

I have looked for this error and tried a few of the solutions, but have not found anything, at this point I would just like to compile it. The error I am getting is:

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start': /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main' 
using namespace std; #include #include #include "math.h" class Solar < int main()< initializeGL(); //Stars Alpha = new Stars(5.0); //Stars *Alpha = new Stars(5.0); //Planets *Awe = new Planets(.6,2,30,"Awe",0.0,0.0,0.0); paintGL(); return 0; >vid initializeGL()< glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // lighting stuff GLfloat ambient[] = ; GLfloat diffuse[] = ; GLfloat specular[] = ; GLfloat position0[] = ; glLightfv( GL_LIGHT0, GL_POSITION, position0 ); glLightfv( GL_LIGHT0, GL_AMBIENT, ambient ); glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse ); glLightfv( GL_LIGHT0, GL_SPECULAR, specular ); GLfloat position1[] = ; glLightfv( GL_LIGHT1, GL_POSITION, position1 ); glLightfv( GL_LIGHT1, GL_AMBIENT, ambient ); glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse ); glLightfv( GL_LIGHT1, GL_SPECULAR, specular ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHT1 ); glEnable( GL_COLOR_MATERIAL ); /* Draws the Grid*/ drawRGrid(); > void resizeGL( int width, int height ) < height = height?height:1; glViewport( 0, 0, (GLint)width, (GLint)height ); // update projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,.10f,200.0f); // modeview matrix is simply identity glMatrixMode(GL_MODELVIEW); glLoadIdentity(); >void paintGL() < glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //set camera position using gluLookAt glLoadIdentity(); gluLookAt(0.0f,0.0f,0.0f,0.0f,0.0f,-200.0f,0.0f,1.0f,0.0f); >void doCircle(double x, double y, double radius) < glEnable(GL_BLEND); double y1=y+radius; double x1=x; glBegin(GL_LINE_STRIP); for(double angle=0.0f;angle<=(2.0f*3.14159);angle+=0.01f)< double x2=x+(radius*(float)sin((double)angle)); double y2=y+(radius*(float)cos((double)angle)); glColor3f(1.0,1.0,1.0); //White glVertex2d(x1,y1); y1=y2; x1=x2; >glEnd(); glDisable(GL_BLEND); > void drawRGrid() < float xCirc = 0; float yCirc = 0; int numCircles = 5; int threesixty = 360; int numLines = 20; //draws my circles for (int i=0; i < numCircles;i++ )< doCircle(1.0,1.0,i); >//draws my lines for (int j=0; j < threesixty / numLines;j+= numLines)< // multiply by numCircles to get to extend to // that length drawLines(sin(j)*numCircles,sin(j)*numCircles); >> void drawLines(float xCirc, float yCirc) < glBegin(GL_LINES); glVertex2f(0,0); glVertex2f(xCirc,yCirc); glEnd(); >>; 

Источник

Читайте также:  Oracle linux server установка

C Linking Error: undefined reference to ‘main’

I have read the other answers on this topic, and unfortunately they have not helped me. I am attempting to link several c programs together, and I am getting an error in response:

$ gcc -o runexp.o scd.o data_proc.o -lm -fopenmp /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status make: * [runexp] Error 1 

your GCC command doesn’t include runexp.c, it OUTPUTS to runexp.o — sure you’re including the source file with the main method ?

5 Answers 5

You should provide output file name after -o option. In your case runexp.o is treated as output file name, not input object file and thus your main function is undefined.

You’re not including the C file that contains main() when compiling, so the linker isn’t seeing it.

$ gcc -o runexp runexp.c scd.o data_proc.o -lm -fopenmp 

You are overwriting your object file runexp.o by running this command :

 gcc -o runexp.o scd.o data_proc.o -lm -fopenmp 

In fact, the -o is for the output file. You need to run :

gcc -o runexp.out runexp.o scd.o data_proc.o -lm -fopenmp 

runexp.out will be you binary file.

Generally you compile most .c files in the following way:

gcc foo.c -o foo. It might vary depending on what #includes you used or if you have any external .h files. Generally, when you have a C file, it looks somewhat like the following:

#include /* any other includes, prototypes, struct delcarations. */ int main() < */ code */ >

When I get an ‘undefined reference to main’, it usually means that I have a .c file that does not have int main() in the file. If you first learned java, this is an understandable manner of confusion since in Java, your code usually looks like the following:

//any import statements you have public class Foo < int main()<>> 

I would advise looking to see if you have int main() at the top.

Источник

Оцените статью
Adblock
detector