【发布时间】:2018-04-14 02:00:44
【问题描述】:
我正在学习使用 Cmake 构建一个库。构建库的代码结构如下:
include:
Test.hpp
ITest.hpp // interface
src:
Test.cpp
ITest.cpp
在CMakeLists.txt中,我用来建库的语句是:
file(GLOB SRC_LIST "src/iTest.cpp" "src/Test.cpp" "include/Test.hpp"
"include/iTest.hpp" "include/deadreckoning.hpp")
add_library(test SHARED ${SRC_LIST})
target_link_libraries( test ${OpenCV_LIBS}) // link opencv libs to libtest.so
然后我又写了一个测试文件(main.cpp),把库复制粘贴到同目录下,链接库并调用库里面的函数。 这个 CMakeLists.txt 是
cmake_minimum_required(VERSION 2.8)
project(myapp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -O3 -Wall -ftree-vectorize -ffast-math -funroll-loops")
add_executable(myapp main.cpp)
target_link_libraries(myapp "/home/labUser/test_lib/libtest.so")
如果我不在库中包含头文件,main.cpp 将成功编译并运行:
#include <iostream>
using namespace std;
int main(){
cout << "hello world" << endl;
return -1;
}
但是当我包含头文件#include "ITest.hpp"时,它有错误:
fatal error: iTest.hpp: No such file or directory
#include "iTest.hpp"
compilation terminated.
我不明白为什么会这样。我想我已经成功链接了库,因为当我运行 main.cpp 而不包含头文件时,它不会给出任何“链接”错误。而且我认为头文件显然在库中。为什么我不能包含它?谁能帮我解决这个问题?
非常感谢!
【问题讨论】:
标签: c++ cmake shared-libraries