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

[亡魂溺海] 提交于 2019-11-26 12:58:31

问题


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)

回答1:


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.




回答2:


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




回答3:


I had similar result when trying to build a new test project with boost, and it turned out that I was missing one declaration :

#define BOOST_TEST_MODULE <yourtestName>



回答4:


I had a similar result when compiling a Fortran program that had C++ components linked in. In my case, CMake failed to detect that Fortran should be used for the final linking. The messages returned by make then ended with

[100%] Linking CXX executable myprogram
/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
make[3]: *** [myprogram] Error 1
make[2]: *** [CMakeFiles/myprogram.dir/all] Error 2
make[1]: *** [CMakeFiles/myprogram.dir/rule] Error 2
make: *** [myprogram] Error 2

The solution was to add

set_target_properties(myprogram PROPERTIES LINKER_LANGUAGE Fortran) 

to the CMakeLists.txt, so that make prints out:

[100%] Linking Fortran executable myprogram
[100%] Built target myprogram


来源:https://stackoverflow.com/questions/11116399/crt1-o-in-function-start-undefined-reference-to-main-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!