【发布时间】:2020-03-08 22:17:48
【问题描述】:
我正在尝试为一个班级开发一个项目,并且我想使用 CMake 来构建该项目。我目前的项目看起来像
|-bin
|-CMakeLists.txt
|-include
|-asio-1.12.2
|-chat_message.hpp
|-chat_message.cpp
|-CMakeLists.txt
|-src
|-Server.cpp
虽然我的 Server.cpp 需要 /include/asio-1.12.2/include 中的 asio.hpp。
教授有一个用标志编译它的makefile
-DASIO_STANDALONE -Wall -O0 -g -std=c++11 -I./include -I./include/asio-1.12.2/include。我的 CMakeLists 文件如下所示:
./CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
PROJECT(Server VERSION 0.0.1)
SET(CPP_STANDARD 11)
SET(CPP_STANDARD_REQUIRED True)
ADD_SUBDIRECTORY(include)
ADD_EXECUTABLE(Server src/Server.cpp)
TARGET_LINK_LIBRARIES(
Server PRIVATE
chat_message
asio
)
./include/CMakeLists.txt
ADD_LIBRARY(
chat_message
chat_message.cpp
chat_message.hpp
)
ADD_LIBRARY(
asio
asio-1.12.2/include/asio.cpp
asio-1.12.2/include/asio.hpp
)
TARGET_INCLUDE_DIRECTORIES(
chat_message PUBLIC "${CMAKE_SOURCE_DIR}/include"
asio PUBLIC "${CMAKE_SOURCE_DIR}/include/asio-1.12.2/include"
)
如何将 asio 头文件链接到带有所需标志的 Server.cpp 文件?
【问题讨论】:
-
请注意,受target_include_directories 命令影响的唯一目标是第一个参数 表示的目标。对于进程两个目标,您需要调用
target_include_directories两次。
标签: cmake boost-asio asio