1

I have a project and the file structure of it looks like this:

|-- CMakeLists.txt
|-- build
|-- include
|-- libs
|   |-- glad
|   |   |-- include
|   |   |   `-- glad
|   |   |       `-- glad.h
|   |   `-- src
|   |       `-- glad.c
|   `-- glfw
|       `-- include
|           `-- GLFW
|               `-- glfw3.h
|-- sandbox
|   |-- CMakeLists.txt
|   |-- build
|   `-- main.cpp
`-- src

and i want to link the project as a static library. the goal is, that at the end i can just say, in the CMakeLists.txt file of sandbox:

target_link_libraries(sandbox <path_to_library>/libsomething.a)

and with that, i also want glad and glfw to link/compile. my current CMakeLists.txt file of the root folder looks like this:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(luna)

find_package(glfw3 3.3 REQUIRED)

file(GLOB_RECURSE SRC_FILES src/*.cpp)
add_library(luna ${SRC_FILES} libs/glad/src/glad.c)

target_link_libraries(luna glfw)

target_include_directories(luna PUBLIC include libs/glad/include libs/glfw/include)

and the CMakeLists.txt in sandbox like this:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(sandbox)

find_package(glfw3 3.3 REQUIRED)

add_executable(sandbox main.cpp)

target_link_libraries(sandbox <absolut_path_to_library>)   

im not expecting that the file structure is perfect at the moment, so tips on improving it would be very welcome.

m100re
  • 88
  • 5
  • 3
    A static library **file** (`libsomething.a` in your case) cannot store information about include directories and linked libraries. So linking an executable with that **file** doesn't give your that information. As opposite, a library **target** in CMake can store all these information and can propagate this information with you link with the **target**. The target for link should either be created in the same CMake project, or created as a part of `find_package` call when it processes the *package configuration file*. https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html – Tsyvarev Jun 30 '22 at 19:17

1 Answers1

0

If you want to use some functional from the sandbox project, you could a little change a sandbox structure.

|-- CMakeLists.txt
|-- build
|-- include
|-- libs
|   |-- glad
|   |   |-- include
|   |   |   `-- glad
|   |   |       `-- glad.h
|   |   `-- src
|   |       `-- glad.c
|   `-- glfw
|       `-- include
|           `-- GLFW
|               `-- glfw3.h
|-- sandbox
|   |-- CMakeLists.txt
|   |-- build
|   |-- common_stuff.cpp
|   |-- common_stuff.h
|   `-- main.cpp
`-- src

In this way, CMakeLists.txt of sandbox can look like this:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(sandbox)

find_package(GLFW3 REQUIRED)

add_library(sandboxLib common_stuff.cpp)
target_link_libraries(sandboxLib ${GLFW3_LIBRARY})
target_include_directories(sandboxLib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(sandboxBin main.cpp common_stuff.cpp)
target_link_libraries(sandboxBin ${GLFW3_LIBRARY})

and the main CMakeList.txt will look like this:

cmake_minimum_required(VERSION 3.13)
cmake_policy(VERSION 3.0)
project(window)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

find_package(GLFW3 REQUIRED)

set(LIBS ${GLFW3_LIBRARY})

add_library(GLAD "libs/glad/src/glad.c")
set(LIBS ${LIBS} GLAD)
include_directories(${CMAKE_SOURCE_DIR}/libs/glad/include)

add_subdirectory(sandbox)
set(LIBS ${LIBS} sandboxLib)

add_executable(lunaBin src/luna/main.cpp)
target_link_libraries(lunaBin ${LIBS})