cmake无法完成我项目中需要的所有静态库

时间:2018-11-05 11:00:25

标签: c++ cmake

我的cmake文件是这样的:

cmake_minimum_required(VERSION 3.12)
project(ICTSentiClfTest)

set(CMAKE_CXX_STANDARD 14)
INCLUDE_DIRECTORIES(
    ~/lyy/icttextminingplatform/include
)

LINK_DIRECTORIES(/usr/local/lib/libpthread.a
    ${PROJECT_SOURCE_DIR}/lib/libGolaxyTAP.a
    ${PROJECT_SOURCE_DIR}/lib/libWordSeg.a
    ${PROJECT_SOURCE_DIR}/lib/libwdselicense.a
    )

add_executable(ICTSentiClfTest src/NewSentiClassify.cpp)

我的项目的lib目录中有cmake所需的所有库,但是在clion调试中,它发生了对XXX错误的未定义引用:

[ 50%] Linking CXX executable ICTSentiClfTest
CMakeFiles/ICTSentiClfTest.dir/src/NewSentiClassify.cpp.o: In function `SentiClassifyAPI(char const*,golaxytap::common::AutoPtr<golaxytap::trainset::DocReader, golaxytap::common::SharedObj<golaxytap::trainset::DocReader> >)':
/home/yiyi/lyy/1/ICTSentiClfTest/src/NewSentiClassify.cpp:21: undefined reference to `Senti_Classifier_Init'
/home/yiyi/lyy/1/ICTSentiClfTest/src/NewSentiClassify.cpp:32: undefined reference to `Senti_Classify_Perform'
/home/yiyi/lyy/1/ICTSentiClfTest/src/NewSentiClassify.cpp:52: undefined reference to `Senti_Classifier_Exit'
CMakeFiles/ICTSentiClfTest.dir/src/NewSentiClassify.cpp.o: In function `CNNSentiClassifyAPI(char const*)':
/home/yiyi/lyy/1/ICTSentiClfTest/src/NewSentiClassify.cpp:60: undefined reference to `Senti_Classifier_Init'
......

我写的makefile:

all:./src/SentiClassify.cpp 
g++ -o NewSentiClassify ./src/NewSentiClassify.cpp -I/home/yiyi/lyy/icttextminingplatform/include -L./lib -lGolaxyTAP -L./lib -lWordSeg -L./lib -lwdselicense -L./usr/local/lib -lpthread

clean:
    rm -f *.o 

使用makefile进行编译不会遇到错误。 为什么?我用谷歌搜索,cmakelist中的link_directories等于g ++中的-L,cmakelist中的include_directories等于g ++中的-I。看来我的cmakelist文件是正确的,并且与g ++编译命令相同。

1 个答案:

答案 0 :(得分:1)

在CMakeLists.txt文件中,应避免使用绝对路径(特别是因为Clion不一定从您的主目录启动cmake)。

INCLUDE_DIRECTORIES(
  <proper-path>/include
)

而且,您似乎不匹配LINK_DIRECTORIES和target_link_library。您应该改用以下语句:

add_executable(ICTSentiClfTest src/NewSentiClassify.cpp)

target_link_libraries(ICTSentiClfTest
  pthread
  lib/libGolaxyTAP.a
  lib/libWordSeg.a
  lib/libwdselicense.a
)

请注意,默认情况下,/ usr / local / lib必须位于LD_LIBRARY_PATH(链接器在进行链接时将查找的路径)上。另外请注意,您应该选择target_include_directories而不是INCLUDE_DIRECTORIES。