CMake在include_directories中找不到正确的头文件/包含文件

lil*_*tt8 4 c++ compilation cmake

当我尝试编译时,再次出现“架构x86_64的未定义符号”错误。我已经做了很多尝试,超出了我在这篇文章中实际记录的范围(因为我已经忘记了所有尝试过的内容)。这是一个非常简单的设置,使用CMake轻松编译。

当我对此进行make时,效果很好。但我想将其转换为CMake以实现互操作性。如您所见,我在一些地方抛出了“ $ {HEADERS}”变量,我已经尝试了很多放置它的位置,但是我不断遇到错误。根据我放置$ {HEADER}的位置,它还可以从技术上生成错误“错误:生成多个输出文件时无法指定-o”(如果它位于target_link_library声明中,则适用)。

我有2个文件夹:

Root
    Headers (contains all .h files)
    Source (contains all .cc/.cpp/.c files) (and also a CMakeLists.txt)
CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)

我的根CMakeLists.txt包含以下内容:

cmake_minimum_required(VERSION 2.8.4)
project(Framework)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_compile_options("-v")

add_subdirectory(Source)

#Variables for making my life easier and adding the headers
set(H Headers)
include_directories(${H})
set(S Source)
file(GLOB HEADERS
#Add any file in the headers dir
"${H}/*"
)

# Create a variable to use for main.cc
set(MAIN ${S}/main.cc ${HEADERS})

# Add the main.cc file and headers
add_executable(Framework ${MAIN} ${HEADERS})

# Add the .cc/.cpp files
target_link_libraries(Framework ${SOURCE_FILES})
Run Code Online (Sandbox Code Playgroud)

我的源目录中的CMakeLists.txt包含以下内容:

file(GLOB SOURCES
"*.cc"
"*.cpp"
)

add_library(SOURCE_FILES ${SOURCES})
Run Code Online (Sandbox Code Playgroud)

我相信标题中没有一个,据我所知,文档指出我们不需要。

谢谢您的帮助。我看了看:

Jiř*_*šil 5

这里的主要问题是,您所引用的SOURCE_FILES目标就像变量一样。除去美元符号和花括号。

target_link_libraries(Framework SOURCE_FILES)
Run Code Online (Sandbox Code Playgroud)

include_directories打电话后设置的设置似乎也很奇怪,add_subdirectory如果可以的话我会感到惊讶。

总的来说,我认为您正在使事情变得复杂。以下是所有必要的步骤。

顶层CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Framework CXX)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic")

include_directories(
  ${PROJECT_SOURCE_DIR}/Headers
)

add_subdirectory(Source)
Run Code Online (Sandbox Code Playgroud)

来源/CMakeLists.txt

# Do not use file globing because then CMake is not able to tell whether a file
# has been deleted or added when rebuilding the project.
set(HELLO_LIB_SRC
  hello.cc
)
add_library(hello ${HELLO_LIB_SRC})

set(MAIN_SRC
  main.cc
)
add_executable(hello_bin ${MAIN_SRC})
target_link_libraries(hello_bin hello)
Run Code Online (Sandbox Code Playgroud)

标头/hello.h

#pragma once

#include <string>

namespace nope
{
  std::string hello_there();
}
Run Code Online (Sandbox Code Playgroud)

来源/hello.cc

#include <hello.h>

namespace nope
{
  std::string hello_there()
  {
    return "Well hello there!";
  }
}
Run Code Online (Sandbox Code Playgroud)

来源/main.cc

#include <hello.h>
#include <iostream>

int main()
{
  std::cout << nope::hello_there() << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

不用担心文件在build文件夹中的放置。这是为了确定安装步骤。

$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make
Run Code Online (Sandbox Code Playgroud)