如何解决如何使用CMake构建一个包括两个目录的可执行文件
我需要编译一个可执行文件,包括client
和common
目录中的所有源文件
\root
\client
*.cpp
*.h
CMakeLists.txt
\common
*.cpp
*.h
这是我当前的CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
project(Client)
set(common_dir ${PROJECT_SOURCE_DIR}/common)
include_directories(${common_dir})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
#set(CMAKE_BUILD_TYPE RELEASE)
if (CMAKE_BUILD_TYPE STREQUAL "RELEASE")
add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif (CMAKE_BUILD_TYPE STREQUAL "RELEASE")
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
IF(WIN32)
SET(OS_SPECIFIC_LIBS netapi32 wsock32)
ENDIF(WIN32)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Gui REQUIRED)
find_package(Qt5 COMPONENTS Network REQUIRED)
#find_package(Qt5 COMPONENTS Sql REQUIRED)
find_package(Qt5 COMPONENTS Svg REQUIRED)
find_package(Qt5 COMPONENTS PrintSupport REQUIRED)
find_package(Qt5WebSockets REQUIRED)
file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")
add_library(common_src)
add_executable(Client ${common_src} ${client_src})
target_link_libraries(Client ${common_dir} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network Qt5::Svg Qt5::PrintSupport Qt5::WebSockets ${OS_SPECIFIC_LIBS})
但是我遇到了这个错误:
mingw32-make.exe[3]: *** No rule to make target '../common',needed by 'Client.exe'. Stop.
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:126: CMakeFiles/Client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:133: CMakeFiles/Client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:150: Client] Error 2
解决方法
任何一个
删除 add_library(common_src)
或
add_library(MyLib ${common_src})
add_executable(Client ${client_src])
target_link_libraries(Client MyLib)
,
似乎您在cmake中混淆了很多东西。在这里,您可以查看源(globbing is bad,don't do it):
src/app
然后您使用file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")
创建一个库:
common_src
然后使用两者 add_library(common_src)
和client_src
创建可执行文件:
common_src
然后尝试将add_executable(Client ${common_src} ${client_src})
与Client
链接。最后一步没有意义,因为common_dir
中已经包含Client
,并且不需要将common_src
库链接到它。
因此,您可以简化cmake并执行以下操作(仅显示相关部分):
common_dir
旁注:
这两个基本都做同样的事情:
#...
file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")
# add_library(common_src) # Remove this line
add_executable(Client ${common_src} ${client_src}) # create Client out of common and client src
target_link_libraries(Client Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network Qt5::Svg Qt5::PrintSupport Qt5::WebSockets ${OS_SPECIFIC_LIBS})
我建议使用set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # remove this
代替上面的内容来设置C ++标准:
set_target_properties
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。