CMake如何访问不同文件夹中的头文件和源文件

时间:2021-02-12 20:55:34

标签: cmake

我有一个如下图所示的代码库。我正在尝试添加一个新的独立可执行文件。 Extent ErrorId Message IncompleteInput ------ ------- ------- --------------- MissingOpenParenthesisAfterKeyword Missing opening '(' after keyword 'while'. False MissingExpressionAfterKeyword Missing expression after 'until' in loop. False main.cpp 文件位于 CMakeLists.txt 中,folder4 需要来自 main.cpp 的代码。

目前我正在使用:

folder3

我现在应该使用 cmake_minimum_required(VERSION 3.10) # set the project name project(Standalone) # add the executable add_executable(StandaloneExe main.cpp) file( GLOB SRCS *.cpp *.h ) 检索头文件和源文件吗?

我只想用最简单的方法来生成这个可执行文件。

enter image description here

1 个答案:

答案 0 :(得分:1)

<块引用>

我现在应该使用文件( GLOB SRCS *.cpp *.h ) 从文件夹 3 中检索头文件和源文件吗?

不,您应该永远使用 GLOB 来获取资源。有关更多详细信息,请在此处查看我的回答:https://stackoverflow.com/a/65191951/2137996

<块引用>

我只想用最简单的方法来生成这个可执行文件。

将您的 CMakeLists.txt 放在根目录中。然后就写:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Standalone)

# add the executable
add_executable(
  StandaloneExe
  folder2/folder4/main.cpp
  folder1/folder3/a.cpp
  folder1/folder3/b.cpp
)

# Might need this, maybe not, depending on your includes
target_include_directories(
  StandaloneExe
  PRIVATE 
    "${CMAKE_CURRENT_SOURCE_DIR}/folder1/folder3"
)

如果你绝对不能移动你的列表文件,那么你可以使用绝对路径:

add_executable(
  StandaloneExe
  ${CMAKE_CURRENT_LIST_DIR}/../../folder2/folder4/main.cpp
  ${CMAKE_CURRENT_LIST_DIR}/../../folder1/folder3/a.cpp
  ${CMAKE_CURRENT_LIST_DIR}/../../folder1/folder3/b.cpp
)
相关问题