CMake not finding proper header/include files in include_directories

本小妞迷上赌 提交于 2019-12-04 09:38:51

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!