Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake finds more than one main function

Tags:

c++

boost

cmake

I am trying to compile a project that has only one main function, but CMake find more.

My CMakeLists.txt is like:

cmake_minimum_required(VERSION 2.8)
project(my_proj)

include_directories(".")

add_subdirectory(main)
add_subdirectory(resources)

find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS system regex program_options)
include_directories(${Boost_INCLUDE_DIRS})

file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE HDR_FILES ${PROJECT_SOURCE_DIR}/*.hpp)

add_executable(my_proj ${SRC_FILES} ${HDR_FILES})

target_link_libraries(my_proj ${OpenCV_LIBS})

target_link_libraries(my_proj ${OpenCV_LIBS} 
                  ${Boost_PROGRAM_OPTIONS_LIBRARY} 
                  ${Boost_REGEX_LIBRARY}
                  ${Boost_FILESYSTEM_LIBRARY}
                  ${Boost_SYSTEM_LIBRARY})

I have more folders with .hpp and .cpp files that is why I have added file(GLOB_RECURSE... statements and also include_directories(".").

I get an error after it compiles all files that says:

CMakeFiles/my_proj.dir/CMakeFiles/CompilerIdCXX/CMakeCXXCompilerId.cpp.o: In    function `main':
/media/N/my_proj/build/CMakeFiles/CompilerIdCXX/CMakeCXXCompilerId.cpp:209: multiple definition of `main'
CMakeFiles/my_proj.dir/main.cpp.o:/media/N/my_proj/main.cpp:10: first defined here
CMakeFiles/my_proj.dir/main/solution2/sources/CRunSolution2.cpp.o: In function `boost::filesystem3::path::codecvt()':
/usr/include/boost/filesystem/v3/path.hpp:377: undefined reference to `boost::filesystem3::path::wchar_t_codecvt_facet()'

Has anyone met something like that? If yes, how to fix it?

like image 688
thedarkside ofthemoon Avatar asked Mar 04 '14 16:03

thedarkside ofthemoon


Video Answer


1 Answers

In your executable you simply have 2 main functions (print out SRC_FILES by MESSAGE(${SRC_FILES})). One is in main.cpp and one in CMakeCXXCompilerId.cpp (which is a file that CMake generates to test if your CXX compiler works correctly). The GLOB_RECURSE probably finds and adds both of these files to SRC_FILES

Using FILE(GLOB ...) is tricky:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

You should list your source and header files in your CMakeLists.txt directly

like image 78
Peter Avatar answered Sep 27 '22 19:09

Peter