本文介绍了cmake找到多个主要函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的CMake是这样的:

  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} )

文件(GLOB_RECURSE SRC_FILES $ {PROJECT_SOURCE_DIR} / *。cpp)
文件(GLOB_RECURSE HDR_FILES $ {PROJECT_SOURCE_DIR} / *。hpp)

add_executable $ {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})

我有更多的文件夹与.hpp和.cpp文件,这就是为什么我添加了文件(GLOB_RECURSE ... 语句和 include_directories 。)



编译所有文件后出现错误:

  CMakeFiles / my_proj.dir / CMakeFiles / CompilerIdCXX / CMakeCXXCompilerId.cpp.o:在main函数中:
/ media / N / my_proj / build / CMakeFiles / CompilerIdCXX /CMakeCXXCompilerId.cpp:209:`main'的多重定义
CMakeFiles / my_proj.dir / main.cpp.o:/media/N/my_proj/main.cpp:10:先定义这里
CMakeFiles /my_proj.dir/main/solution2/sources/CRunSolution2.cpp.o:在函数`boost :: filesystem3 :: path :: codecvt()':
/ usr / include / boost / filesystem / v3 / path .hpp:377:未定义引用`boost :: filesystem3 :: path :: wchar_t_codecvt_facet()'

有没有人遇到过类似的东西?

解决方案

在您的可执行文件中,只需要两个主要函数(打印出 SRC_FILES MESSAGE($ {SRC_FILES}))。一个是在 main.cpp 和一个在 CMakeCXXCompilerId.cpp (这是一个CMake生成的文件, CXX编译器工作正常)。 GLOB_RECURSE 可能会找到并将这两个文件添加到 SRC_FILES



使用比较棘手:

您应该直接在CMakeLists.txt中列出源文件和头文件


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

My CMake 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?

解决方案

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:

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

这篇关于cmake找到多个主要函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 12:06
查看更多