Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crt1.o: In function `_start': - undefined reference to `main' in Linux

I am porting an application from Solaris to Linux

The object files which are linked do not have a main() defined. But compilation and linking is done properly in Solaris and executable is generated. In Linux I get this error

    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main'  

My problem is, I cannot include new .c/.o files since its a huge application and has been running for years. How can I get rid of this error?

Code extractes of makefile:

RPCAPPN = api LINK = cc       $(RPCAPPN)_server: $(RPCAPIOBJ)             $(LINK) -g $(RPCAPIOBJ) -o $(RPCAPPN)_server $(IDALIBS) $(LIBS) $(ORALIBS) $(COMMONLIB) $(LIBAPI) $(CCLIB) $(THREADLIB) $(DBSERVERLIB) $(ENCLIB) 
like image 672
Blackforest Avatar asked Jun 20 '12 09:06

Blackforest


People also ask

How do you fix undefined references to Main?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).

What is __ Dso_handle?

__dso_handle is a "guard" that is used to identify dynamic shared objects during global destruction.

What does undefined reference to main mean in C++?

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


1 Answers

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

$(LINK) -nostartfiles -g ... 

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.

like image 109
Paul R Avatar answered Sep 24 '22 22:09

Paul R