首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CMake在同一级别构建一个包含两个目录的可执行文件

如何使用CMake在同一级别构建一个包含两个目录的可执行文件
EN

Stack Overflow用户
提问于 2020-08-19 19:25:58
回答 2查看 55关注 0票数 0

我需要编译一个可执行文件,其中包括clientcommon目录中的所有源文件

代码语言:javascript
复制
\root
 \client
   *.cpp
   *.h
   CMakeLists.txt
 \common
   *.cpp
   *.h

这是我当前的CMakeLists.txt

代码语言:javascript
复制
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})

但是我得到了这个错误:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-08-19 19:35:18

任一

删除add_library(common_src)

代码语言:javascript
复制
add_library(MyLib ${common_src})
add_executable(Client ${client_src])
target_link_libraries(Client MyLib)
票数 1
EN

Stack Overflow用户

发布于 2020-08-19 19:42:21

看起来你把很多东西都弄混了。你可以在这里找到源代码(globbing is bad, don't do it):

代码语言:javascript
复制
file(GLOB client_src "*.h" "*.cpp" "Resources.qrc")
file(GLOB common_src "${common_dir}/*.h" "${common_dir}/*.cpp")

然后在common_src之外创建库

代码语言:javascript
复制
add_library(common_src)

然后使用创建可执行文件,包括 client_srccommon_src

代码语言:javascript
复制
add_executable(Client ${common_src} ${client_src})

然后尝试将Clientcommon_dir链接起来。最后一步没有意义,因为Client中已经有了common_src,不需要将common_dir库链接到它。

因此,您可以简化cmake并执行以下操作(只显示相关部分):

代码语言:javascript
复制
#...
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})

附注:

这两者本质上做的是一样的事情:

代码语言:javascript
复制
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # remove this

相反,我使用set_target_properties重新注释来设置C++标准,而不是上述内容:

代码语言:javascript
复制
set_target_properties(Client PROPERTIES
    CXX_STANDARD 17           # standard version
    CXX_STANDARD_REQUIRED ON  # required yes
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63485977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档