1. 說明
使用clang++10.1編譯報錯:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/main.dir/build.make:104: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:97: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
2.分析
- 2.1 編譯器提示的很清楚: 沒有main函數。
- 2.2 我的CMakeLists.txt代碼:
......
## 這段代碼是報錯提示相關的 ##
add_executable(main ${sp_src} ${sp_inc})
target_sources(main PRIVATE ${sp_src} ${sp_inc} )
....
說明: ${sp_src}的源文件代碼中,沒有main函數。 因為這里是要創建一個可執行程序,所以修改方式: 將正確的含入口函數main的源文件包含進來即可。
- 2.3 改進
file(GLOB_RECURSE main_src ${CMAKE_CURRENT_SOURCE_DIR}/exam/*.cpp)
add_executable(main ${main_src} ${sp_inc})
# .h and .cxx files
target_sources(main PRIVATE ${main_src} ${sp_inc} )