CM在include_directories中找不到合适的头/包含文件

时间:2014-11-14 19:02:51

标签: 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

我的根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})

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

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

add_library(SOURCE_FILES ${SOURCES})

我的标题中没有一个,我相信,文档说明我们不需要。

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

1 个答案:

答案 0 :(得分:4)

这里的主要问题是您将SOURCE_FILES目标称为变量。删除美元符号和大括号。

target_link_libraries(Framework SOURCE_FILES)

在致电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)

<强>源/的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)

<强>页眉/ hello.h

#pragma once

#include <string>

namespace nope
{
  std::string hello_there();
}

<强>源/ hello.cc

#include <hello.h>

namespace nope
{
  std::string hello_there()
  {
    return "Well hello there!";
  }
}

<强>源/ main.cc

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

int main()
{
  std::cout << nope::hello_there() << std::endl;
  return 0;
}

不要担心在build文件夹中放置文件。这就是安装步骤的要点。

$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make