L.Elizabeth
L.Elizabeth

Reputation: 157

cmake unsuccessful with all static libraries needed in my project

My cmake file is like this:

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)

I have all libs that cmake needs in the lib dir of my project, but in clion debug, it occurs undefined reference to XXX error:

[ 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'
......

The makefile I wrote:

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 

Using makefile to compile does not encounter error. Why? I googled that link_directories in cmakelist equals to -L in g++ and include_directories in cmakelist equals to -I in g++. It seems like my cmakelist file is correct and same with g++ compile orders.

Upvotes: 2

Views: 704

Answers (1)

Gergely Nyiri
Gergely Nyiri

Reputation: 345

In your CMakeLists.txt file you should avoid absolute paths (especially since Clion will not necessarily launch cmake from your home directory).

INCLUDE_DIRECTORIES(
  <proper-path>/include
)

Also, you seem to mismatch LINK_DIRECTORIES and target_link_library. You should use below statements instead:

add_executable(ICTSentiClfTest src/NewSentiClassify.cpp)

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

Note that /usr/local/lib must be on the LD_LIBRARY_PATH by default (the path that the linker will looking at when doing the linkage). Also note that you should prefer target_include_directories instead of INCLUDE_DIRECTORIES.

Upvotes: 1

Related Questions