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

后端 未结 5 887
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 02:50

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 Sol

相关标签:
5条回答
  • 2020-12-01 03:13

    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>
    
    0 讨论(0)
  • 2020-12-01 03:16

    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
    
    0 讨论(0)
  • 2020-12-01 03:25

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

    0 讨论(0)
  • 2020-12-01 03:34

    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.

    0 讨论(0)
  • 2020-12-01 03:37

    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.

    Hope this helps anyone :)

    0 讨论(0)
提交回复
热议问题