这里的主要问题是你指的是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;
}
不必担心构建文件夹中文件的放置。这是安装步骤需要弄清楚的。
$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make