如何在 Cmake / Kdevelop 中使用/包含库

How to use / include libraries in Cmake / Kdevelop

我不明白我需要做什么才能使用位于 /usr/include 的库。

例如:我想使用位于/usr/include/json的json library。 在我的项目中 'main.cpp' 我做 #include <json/json.h>.

我没有得到任何错误,但是当我开始使用库中的函数时,我得到了未定义的引用错误。我在使用多个库时遇到了这个问题,我不知道该怎么做我在 google 上进行了搜索,但我只遇到了模糊的答案。

我很确定我需要在 CMakeLists.txt 文件中做一些事情,但我不知道是什么。

默认情况下,

/usr/include 可用于包含。但是当你包含一个外部库时,你必须 link 它到你的目标。如果您使用 cmake,可以按如下方式完成:将以下行添加到您的 CMakeLists.txt:

target_link_libraries(your_target_name your_library_name)

例如,在我的机器 (Fedora 21) 上,jsoncpp 包名为 jsoncpp,它的 include 个文件在 /usr/include/jsoncpp/json 中。所以我这样创建 test.cpp

#include <jsoncpp/json/json.h>
#include <iostream>

int main(int, char**)
{
    Json::Value val(42);
    Json::StyledStreamWriter sw;
    sw.write(std::cout, val);   
    return 0;
}

CMakeLists.txt

add_executable(test
test.cpp
)

target_link_libraries(test jsoncpp)

一切正常。