More GCC link time issues: undefined reference to main
I’m writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I’m making use of the GNU compilers and related toolchains; these tools are installed on the processor board (Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC (Windows) using WinSCP and the PuTTY terminal. As usual I started with a simple C project having main.c and functions.s. I compile the main.c using GCC, assemble the functions.s using as and link the generated object files using once again GCC, but I get strange errors during this process. An important finding — Meanwhile, I found out that my assembly code may have some issues because when I individually assemble it using the command as -o functions.o functions.s and try running the generated functions.o using ./functions.o command, the Bash shell is failing to recognize this file as an executable (on pressing tab functions.o is not getting selected/PuTTY is not highlighting the file). Can anyone suggest what’s happening here? Are there any specific options I have to send, to GCC during the linking process? The errors I see are strange and beyond my understanding, I don’t understand to what the GCC is referring. I’m pasting here the contents of main.c, functions.s, the Makefile and the list of errors.
Help, please.
ubuntu@ubuntu-desktop:~/Documents/Project/Others/helloworld$ make gcc -c -mcpu=cortex-a8 main.c as -mcpu=cortex-a8 -o functions.o functions.s gcc -o hello main.o functions.o functions.o: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here collect2: ld returned 1 exit status make: *** [hello] Error 1
#include #include int main(void) < puts(". Hello World. "); /* prints . Hello World. */ return EXIT_SUCCESS; >
* Main program */ .equ STACK_TOP, 0x20000800 .text .global _start .syntax unified _start: .word STACK_TOP, start .type start, function start: movs r0, #10 movs r1, #0 .end
all: hello hello: main.o functions.o gcc hello -o main.o functions.o
— hello was included here after suggested here by guys at Stack Overflow, but the problem still persists; I still get the same errors.
main.o: main.c gcc -c -mcpu=cortex-a8 main.c functions.o: functions.s as -mcpu=cortex-a8 -o functions.o functions.s
ubuntu@ubuntu-desktop:~/Documents/Project/Others/helloworld$ make gcc -c -mcpu=cortex-a8 main.c as -mcpu=cortex-a8 -o functions.o functions.s gcc -o main.o functions.o functions.o: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o:init.c:(.text+0x0): first defined here /usr/lib/gcc/arm-linux-gnueabi/4.3.3/../../../crt1.o: In function `_start': init.c:(.text+0x30): undefined reference to `main' collect2: ld returned 1 exit status make: *** [hello] Error 1
C++ Error: undefined reference to `main’
I’m working on a simple class List, but when compiling the header and cpp file, I get the error: undefined reference to `main’ What am I doing wrong, and how could I fix this? Here is the list.h file that has simple headers: list.h
#ifndef LIST_H #define LIST_H #include const int DEFAULT_CAPACITY = 100; class List < public: List(); List(int capacity); ~List(); void push_back(std::string s); int size() const; std::string at(int index) const; private: std::string* mData; int mSize; int mCapacity; >; #endif
#include "list.h" #include List::List()< mData = new std::string[DEFAULT_CAPACITY]; mSize = 0; mCapacity = 100; >; List::List(int capacity)< mData = new std::string[capacity]; mSize = 0; mCapacity = capacity; >; List::~List()< delete[] mData; >; void List::push_back(std::string s) < if (mSize>; int List::size() const< return mSize; >; std::string List::at(int index) const< return mData[index]; >;
I tried experimenting around with «using namespace std» and how to include , but I can’t figure out how to get these errors to go away. What is causing them?
Unless your program is not an executable, the main function is mandatory. You have to create the main function to build the application.
2 Answers 2
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).
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.
Why am I getting «undefined reference to main»
I am a very new to programming and have a very basic question that may be answered in other threads however I think they are far too advanced for me to understand how. I have actually found many answers so far on this site but this is the first problem that forced me to create an account and ask. Anyway i am running a very basic example program on linux mint 18.3. Now I have seen this exact code work on a machine with windows 8 I believe so I was wondering if that could be the problem. I have created a class and when i plug in my object then build and run I get: /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o||In function _start’:| (.text+0x20)||undefined reference to main’| This is the entire code:
#include #include "Gladius.h" using namespace std; int main()
#ifndef GLADIUS_H #define GLADIUS_H class Gladius < public: Gladius(); >; #endif // GLADIUS_H
#include "Gladius.h" #include using namespace std; Gladius::Gladius()
I know this seems extremely simple but I am just learning to code and i have been looking all over for an explanation why this isn’t working yet I see it work on another pc exactly as is. Anyway any explanation would be greatly appreciated. Here is what i found in command line If this answers your questions about what was in the cmd. g++ -Wall -fexceptions -g -std=c++11 -Wall -I -c /home/gator/Documents/Spartan1/Gladius.cpp -o obj/Debug/Gladius.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start’: (.text+0x20): undefined reference to main’ collect2: error: ld returned 1 exit status
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(); >>;
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.