Linker cannot find user library when building from CLion IDE
已回答
Hi,
I'm trying to build existing project from CLion IDE but ld cannot find my custom libraries .
/usr/bin/ld: cannot find -lmylib
The project can be successfully built from terminal with cmake . && make.
The project structure is:
Project
App1
Src
CMakeLists.txt
App2
Src
CMakeLists.txt
MyLibs
mylib
CMakeLists.txt
My custom libraries are static ones and located in MyLibs folder
The CMakeLists.txt of App1:
cmake_minimum_required(VERSION 2.8)
project(App1)
add_definitions(-D_GTEST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Make")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -g -ggdb -D_LINUX -static -DSTDCXX_98_HEADERS -Wno-deprecated-declarations")
set(CMAKE_EXE_LINKER_FLAGS "-static")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_EXE_LINKER_FLAGS "-static")
file(GLOB_RECURSE SOURCES Src/*.cpp)
file(GLOB_RECURSE HEADERS Src/*.h)
MESSAGE(warning, ${LIBS_DIR})
add_definitions(-D_BUILD_NUMBER=${BUILD_NUMBER})
set(LIBS_DIR "../MyLibs")
set(MYLIB_DIR ${LIBS_DIR}/mylib)
include_directories(Src
${MYLIB_DIR}/include)
link_directories(
${MYLIB_DIR}/lib)
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
target_link_libraries(${PROJECT_NAME} mylib)
set(CMAKE_VERBOSE_MAKEFILE ON)
I'm using CLion 2017.2.3 with bundled toolchains on ubuntu 16.04 LTS.
Could you help me with this problem, please?
请先登录再写评论。
Hi!
Sorry for the delay. Please consider using the find_libarary() command which basically takes a list of paths as a hint and then returns an absolute path to the library. With your solution, you might start with making the link_directories() argument an absolute path, however, this approach is considered less convenient than find_library() one.