2017-11-11 8 views
0

我想從我的CLion項目(在Ubuntu上)調用rrd func,rrd.h在/ usr/include /中。從c文件調用rrd_create

我的代碼:

#include <stdio.h> 
#include <rrd.h> 

int main() { 
    size_t argc = 6; 
    char *argv[] = { 
      "test.rrd", 
      "--start", 
      "920804400", 
      "DS:speed:COUNTER:600:U:U", 
      "RRA:AVERAGE:0.5:1:24", 
      "RRA:AVERAGE:0.5:6:10" 
    }; 
    rrd_create(argc, argv); 
    return 0; 
} 

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.8) 
project(rddtool) 

set(CMAKE_C_STANDARD 11) 

set(SOURCE_FILES main.c /usr/include/rrd.h) 
add_executable(rddtool ${SOURCE_FILES}) 

消息生成:

Scanning dependencies of target rddtool 
[ 50%] Building C object CMakeFiles/rddtool.dir/main.c.o 
[100%] Linking C executable rddtool 
CMakeFiles/rddtool.dir/main.c.o: In function `main': 
/home/parallels/CLionProjects/rddtool/main.c:14: undefined reference to `rrd_create' 
collect2: error: ld returned 1 exit status 
CMakeFiles/rddtool.dir/build.make:94: recipe for target 'rddtool' failed 
make[3]: *** [rddtool] Error 1 
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rddtool.dir/all' failed 
make[2]: *** [CMakeFiles/rddtool.dir/all] Error 2 
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rddtool.dir/rule' failed 
make[1]: *** [CMakeFiles/rddtool.dir/rule] Error 2 
Makefile:118: recipe for target 'rddtool' failed 
make: *** [rddtool] Error 2 

我應該如何調用rrd_create?

+2

您的代碼可能是好的,但是'CMakeList.txt'缺少一個非常關鍵的東西:*** ***鏈接與庫。閱讀['target_link_libraries'](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)命令。 –

回答

0

在的CMakeLists.txt:

cmake_minimum_required(VERSION 3.8) 
project(rddtool) 

set(CMAKE_C_STANDARD 11) 

set(SOURCE_FILES main.c) 

find_library(RRD_LIBRARY librrd.so HINTS /usr/lib/x86_64-linux-gnu) 

add_executable(rddtool ${SOURCE_FILES}) 

target_link_libraries(rddtool PUBLIC ${RRD_LIBRARY})