CMake在include_directories中找不到正确的头文件/包含文件 [英] CMake not finding proper header/include files in include_directories

查看:244
本文介绍了CMake在include_directories中找不到正确的头文件/包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次,当我尝试编译时,出现未定义的体系结构x86_64符号错误。我已经做了很多尝试,超出了我在这篇帖子中实际记录的范围(因为我已经忘记了所有尝试过的内容)。这是一个非常简单的设置,并且应该使用CMake可以非常容易地进行编译...



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



我有2个文件夹:

 
标头(包含所有.h文件)
源文件(包含所有.cc / .cpp / .c文件)(以及CMakeLists.txt)
CMakeLists.txt

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

  cmake_minimum_required(版本2.8.4)
项目(框架)

设置(CMAKE_CXX_FLAGS $ {CMAKE_CXX_FLAGS} -std = c ++ 11)
add_compile_options( -v)

add_subdirectory(来源)

#使我的生活更加轻松并添加标题
set(H Headers)
include_directories( $ {H})
set(S源)
文件(GLOB HEADERS
#在标题dir
中添加任何文件 $ {H} / *


#创建一个变量用于main.cc
set(MAIN $ {S} /main.cc $ {HEADERS})

#添加main.cc文件和标题
add_executable(Framework $ {MAIN} $ {HEADERS})

#添加.cc / .cpp文件
target_link_libraries(框架$ {SOURCE_FILES})

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

  file(GLOB源
* .cc
* .cpp


add_library(SOURCE_FILES $ {SOURCES})

按照我的说法,我认为标题中没有一个,我们认为不需要。 / p>

感谢您的帮助。我看过:




解决方案

此处的主要问题是您指的是 SOURCE_FILES target,就好像它是一个变量。删除美元符号和花括号。

  target_link_libraries(Framework SOURCE_FILES)

如果您在调用 add_subdirectory 之后设置了 include_directories ,我会感到惊讶,如果这样做有效。



总体而言,我认为您正在使事情变得比他们需要的复杂。



顶级CMakeLists.txt

  cmake_minimum_required(版本2.8)
项目(框架CXX)

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

include_directories(
$ {PROJECT_SOURCE_DIR} / Headers


add_subdirectory(Source)

Source / CMakeLists.txt

 #请勿使用文件查看,因为CMake不能在重建项目时告诉文件
#是否已删除或添加。
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.h

  #pragma一次

#include< string>

名称空间nope
{
std :: string hello_there();
}

来源/hello.cc

  #include< hello.h> 

命名空间为
{
std :: string hello_there()
{
return Well hello there!;
}
}

来源/main.cc

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

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

不要担心文件在build文件夹中的位置。

  $ mkdir build&& cd build 
$ cmake -DCMAKE_BUILD_TYPE =调试..
$ make


Once again I am getting a "undefined symbols for architecture x86_64" error when I try to compile. I've tried more than I can actually document in this post (because I've forgotten all that I have tried). It is a really simple set-up and should compile very easily with CMake...

When I run a make on this it works just fine. But I want to convert it to CMake for interoperability. As you can see I have thrown my "${HEADERS}" variable in a few places, I've tried quite a few placements of it but I keep getting my error. Depending on where I put that ${HEADER} it can also technically generate an error of "error: cannot specify -o when generating multiple output files" (That applies if it only sits in the target_link_library declaration).

I have 2 folders:

Root
    Headers (contains all .h files)
    Source (contains all .cc/.cpp/.c files) (and also a CMakeLists.txt)
CMakeLists.txt

My root CMakeLists.txt contains the following:

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})

My CMakeLists.txt in my source directory contains the following:

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

add_library(SOURCE_FILES ${SOURCES})

I do not have one in the headers as per, what I believe, the documentation states we don't need.

Thanks for the help. I've looked at:

解决方案

The main problem here is that you're referring to the SOURCE_FILES target as if it was a variable. Remove the dollar sign and curly braces.

target_link_libraries(Framework SOURCE_FILES)

It also seems kind of weird that you set include_directories after calling add_subdirectory, I'd be surprised if that worked.

Overall I think you're making things more complicated than they need to be. The following should be all that's necessary.

Top level 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)

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)

Headers/hello.h

#pragma once

#include <string>

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

Source/hello.cc

#include <hello.h>

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

Source/main.cc

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

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

Do not worry about the placement of files in the build folder. That's for the install step to figure out.

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

这篇关于CMake在include_directories中找不到正确的头文件/包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆