我目前正在尝试弄清楚如何在CMake中链接库。据我所知,我已正确设置了所有内容。但是,当我尝试构建时,出现以下错误。
test/src/test.cpp:1:10: fatal error: lib.h: No such file or directory
#include "lib.h"
^~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/a.out.dir/build.make:63:
CMakeFiles/a.out.dir/src/test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:110: CMakeFiles/a.out.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我的文件结构是
CMakeLists.txt
\src
test.cpp
\lib
lib.h
lib.cpp
这是我的文件
CMakeLists.txt:
cmake_minimum_required(VERSION 2.9 FATAL_ERROR)
project("test")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_library(lib STATIC src/lib/lib.cpp)
add_executable(a.out src/test.cpp )
target_link_libraries(a.out lib)
src / test.cpp:
#include "lib.h"
int main(){
hello();
return 0;
}
src / lib / lib.h
#include <iostream>
void hello();
和src / lib / lib.hpp
#include "lib.h"
void hello(){
std::cout << "Hello World" << std::endl;
}
除非我弄错了,否则只要您进行静态链接,就不必
#include "path/to/file/file.h"
你可以
#include "file.h"
答案 0 :(得分:1)
包含标头的目录不在包含路径中;您可以使用
target_include_directories(a.out PUBLIC ${CMAKE_SOURCE_DIR}/src/lib)
指定要添加到二进制编译的路径
有关语法的更多详细信息,请参见https://cmake.org/cmake/help/v3.0/command/target_include_directories.html。
答案 1 :(得分:-1)
添加到CMakeList.txt:
include_directories("./src/lib")