C++ 链接第三方库

直接使用g++

  • files: main.cpp help.cpp help.h
  • complie: g++ -c *.cpp -I第三方库头文件目录 # -I is not necessary if include path is in system INCLUDPATH
  • link: g++ -o “main” *.o -L第三方链接库目录 -lopencv_core -lopencv_imgproc # -L is not necessary if …; opencv_core means libopencv_core.so

使用CMake

  • 命名变量

    1
    2
    3
    set(INC_DIR /usr/local/include)
    set(LINK_DIR /usr/local/lib)
    set(SOURCE_FILES main.cpp)
  • 去哪里找头文件 相当于gcc/clang 中的-I(i的大写字母)参数

    1
    include_directories(${INC_DIR})     # 用${}引用变量
  • 去哪里找库文件 .so .dll .dylib 相当于gcc 中的-L参数

    1
    link_directories(${LINK_DIR})
  • 要链接的库文件的名字 相当于gcc中的-l(小写的l)参数

    1
    target_link_libraries(test_boost boost_filesystem boost_system)