3

I am currently creating a set of classes that should be used in different projects using CLion. My question is how do I implement this functionality.

So far I looked into the following related issues which didn't really solve my problem:

I created two sample projects "TestLib" and "TestProj":

  • TestLib
    • src
      • Class.h
      • Class.cpp
    • CMakeList.txt
  • TestProj
    • main.cpp
    • CMakeList.txt

The CMakeList.txt for "TestLib" currently looks as follows:

 cmake_minimum_required(VERSION 3.5)
 project(TestLib)

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")

 set(SOURCE_FILES src/Class.cpp src/Class.h)
 add_library(TestLib ${SOURCE_FILES})> 

Now, I tried to use this library in "TestProj" using the following CMakeLists.txt:

 cmake_minimum_required(VERSION 3.5)
 project(TestProj)

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
 find_library(CLASS_LIB TestLib HINTS /home/user/.CLion2016.1/system/cmake/generated/TestLib-7507f101/7507f101/Debug)

 set(SOURCE_FILES main.cpp)
 add_executable(TestProj ${SOURCE_FILES})

 target_link_libraries(TestProj CLASS_LIB)

CMake finds the library but

  1. I dont have access to Class.h of the library
  2. Writing the whole /home/user/.CLion2016.1/...-Path to the library seems to be wrong

Any help is really appreciated. Thank you.

  • Possible duplicate of [CMake link to external library](http://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – usr1234567 Jan 28 '17 at 13:17

2 Answers2

0

You can put at the root of your projects CMakeLists.txt with such content:

add_subdirectory(TestLib)
add_subdirectory(TestProj)

after that you can write in TestProj's CMakeLists.txt just

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../TestLib)
target_link_libraries(TestProj TestList)

and no need to use find_library

fghj
  • 8,898
  • 4
  • 28
  • 56
0

If the goal is to make your library available to multiple other projects, the recommended way to do that would be to have your library project's install procedure generate CMake configuration files. Unfortunately, the procedure to do that is a bit arcane, but https://cmake.org/cmake/help/git-master/manual/cmake-packages.7.html#creating-packages should give you a starting point on how to do this.

Daniel Schepler
  • 3,043
  • 14
  • 20