0

I have been working on a fun project (a game engine) for awhile now and figured i could make it more portable and easy to compile across platforms if I use cmake. Right now i have it set up like so, with a main executable and then a bunch of shared libraries that the executable is linked to. I've found all the material needed to produce the libraries and the executable, and linking those to the executable, but what of linking a dependency like a static library or another shared library to one of the libraries i produce? Here is a visual

Sentiment (name of project) -Root (all the interfaces and components of the engine. main.cpp is here -Sentiment_OGL4Renderer (the files for the Renderer library) -Sentiment_SFMLGui (the files for the Gui library) -Sentiment_TestGame (the code for a game) 

now i want all of these, the executable and the shared libraries built and put into the bin folder in the top level directory. What i found suggested online for a setup like this was to make cmakelists.txt files in each folder, and then one in the root, for each project. What i have thus far is this.

#Sentiment cmake_minimum_required(VERSION 2.6) project(Sentiment) set(RENDERER Sentiment_OGL4Renderer) set(GUI Sentiment_SFMLGui) set(GAME Test_Game) add_definitions(-DBUILD_DLL) list( APPEND CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs") set(EXECUTABLE_OUTPUT_PATH "${Sentiment_SOURCE_DIR}/bin") set(LIBRARY_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}") link_directories("${LIBRARY_OUTPUT_PATH}") add_subdirectory("${RENDERER}") add_subdirectory("${GUI}") add_subdirectory("${GAME}") add_subdirectory(Root) 

in root

project(Sentiment_exe) link_directories("${Sentiment_SOURCE_DIR}/bin") AUX_SOURCE_DIRECTORY(. new_source_list) add_executable("${CMAKE_PROJECT_NAME}" ${new_source_list}) target_link_libraries("${CMAKE_PROJECT_NAME}" "${LIBRARY_OUTPUT_PATH}/${RENDERER}" "${LIBRARY_OUPUT_PATH}/${GUI}" "${LIBRARY_OUTPUT_PATH}/${GAME}" "${ADDITIONAL_DEPENDENCIES}") 

in Sentiment_OGL4Renderer

project(Sentiment_OGL4-3Renderer) include_directories(${Sentiment_SOURCE_DIR}) add_definitions(-DGLEW_STATIC) add_library(Sentiment_OGL4-3Renderer SHARED Sentiment_OGL4Renderer.cpp GL/glew.cpp) 

in Sentiment_SFMLGui

project(Sentiment_SFMLGui) include_directories(${Sentiment_SOURCE_DIR}) add_library(Sentiment_SFMLGui SHARED Sentiment_SFMLGui.cpp) 

in Sentiment_TestGame

project(Sentiment_Game) include_directories(${Sentiment_SOURCE_DIR}) add_library(Sentiment_Game SHARED Game.cpp) 

As you can tell there are a lot of third party libraries, and i tried various methods of linking, like with target_link_libraries, and i cannot for the life of me figure how to link an external library to the ones i've made. First off, the renderer uses GLEW but it needs no external dependency so ignore that. Then it needs OpenGL32.lib and Gdi32.lib from the windows sdk (only for windows). As for SFML, i've got the DLL's in the bin folder which need to be linked, (can easily get the .so's when working in linux and can distribute with the final product if I ever choose to do so). I need these all linked as dependencies to the libraries i create, but nothing seems to work. The project is all c++ and I am currently using mingw32 to compile it. I'm brand new to cmake so please be gentle if it is really simple.

4
  • cmake.org/Wiki/CMake:How_To_Find_Libraries Commented Feb 5, 2014 at 7:26
  • Can you post the error which you are getting ? Commented Feb 5, 2014 at 9:06
  • @Peter - aren't modules like that only for projects that already use cmake? These are just some libraries from the Windows SDK, and some in the project already, i don't think they distribute these SDK's with cmake builds in mind :P. Commented Feb 5, 2014 at 22:34
  • @123r789 - The cmake executes perfectly fine, but the makefile obviously fails because nothing is linked in the renderer. It is missing the wgl function definitions in windows to create a render context because they are in OpenGL32.lib. My question is how to link that into the library i create here. target_link_libraries only seems to link anything when an executable is made, not a library. Commented Feb 5, 2014 at 22:37

1 Answer 1

1

To link external libraries, best practice is to use or create FindModule for given external library.

CMake comes with numerous modules that aid in finding various well-known libraries and packages.

The list of standard modules is in the official documentation

In case there is no standard module for your external library, you should write your own.

The OpenGL library has standard module FindOpenGL:

find_package (OpenGL) if (OPENGL_FOUND) include_directories(${OPENGL_INCLUDE_DIR}) target_link_libraries (Sentiment_OGL4-3Renderer ${OPENGL_gl_LIBRARY}) endif (OPENGL_FOUND) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.