0

I need to add FLTK as a dependency in my CMakeLists.txt. The library based in <project_root/dist/FLTK>. It was built in <project_root/dist/FLTK/build> directory.

This is my hole config:

cmake_minimum_required(VERSION 2.8)

set(DIST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dist")
set(FLTK "${DIST_DIR}/FLTK")

if(NOT IS_DIRECTORY ${FLTK}) 
  message("Starting add FLTK library...")
  execute_process(COMMAND git submodule add https://github.com/IngwiePhoenix/FLTK.git "${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK") 
  execute_process(COMMAND git submodule update --init --recursive)
endif()

link_directories("${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK/build")
add_library(fltk "${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK/build/lib")


add_definitions(-Werror -std=c++17)
add_executable(main main.cpp)

What is wrong? Thanks.

  • Your CMakeLists.txt has several problems. The most obvious one is the use of `add_library` which is for building libraries not for linking (if this is what you wanted to do). A good starting point could be the [CMake tutorial](https://cmake.org/cmake-tutorial/#s2). – havogt Aug 12 '18 at 17:43
  • "It was built in directory." - Has the library **already** been built, or it is *your code* (`CMakeLists.txt`) which should build it? – Tsyvarev Aug 12 '18 at 18:01
  • yes, @Tsyvarev, it was built –  Aug 12 '18 at 19:57
  • Have you seen the question https://stackoverflow.com/questions/8774593/cmake-link-to-external-library about linking the library? – Tsyvarev Aug 12 '18 at 20:01

1 Answers1

1

Thanks to havogt and Tsyvarev I found the solution! I am not expert in CMake but maybe this help to some one:

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++17")

set(DIST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dist")
set(FLTK "${DIST_DIR}/FLTK")

if(NOT IS_DIRECTORY ${FLTK}) 
  message("Starting add FLTK library...")
  execute_process(COMMAND git clone https://github.com/fltk/fltk.git ${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK)
  execute_process(COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK)

add_executable(keyplay ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

find_library(LibFltk libfltk.a PATHS ${CMAKE_CURRENT_SOURCE_DIR}/dist/FLTK/lib/)

if(APPLE)
  find_library(COCOA Cocoa)
endif()

target_link_libraries(keyplay ${LibFltk} ${COCOA})