0

I've never used cmake before, but am now forced by circumstance to port my Visual Studio 2013 OpenGL project into it. I, however, cannot seem to figure out how to link libraries (or even header only library like GLM) to it from a folder. My directory hierarchy is such:

project
|--src
|----main.cpp
|----functions.h
|----functions.cpp
|----...
|--libraries
|----GLM
|------glm (<--the include directory)
|----glew-2.0.0
|------include
|------lib
|----GLFW
|------include
|------lib-vc2013

There seems to be a lot of advice on how one should use system paths and variables to install and link such libraries, but such solutions are useless to me, as it is necessary for the libraries to be shipped together with the source files, as I cannot expect other machines to have these libraries installed or demand them to install libraries outside the project directory. I should also mention that I am using OS Windows 7.

EDIT: This is my CMakeLists.txt file where the libraries are linked:

cmake_minimum_required(VERSION 3.0)
ADD_EXECUTABLE( water_surface main.cpp functions.h functions.cpp)

TARGET_LINK_LIBRARIES(water_surface opengl32)

link_directories(
    ${CMAKE_SOURCE_DIR}/libraries/glew-2.0.0/lib/release/Win32
)
TARGET_LINK_LIBRARIES(water_surface glew32)
include_directories(
    ${CMAKE_SOURCE_DIR}/libraries/glew-2.0.0/include
)

link_directories(
    ${CMAKE_SOURCE_DIR}/libraries/GLFW/lib-vc2013
)
TARGET_LINK_LIBRARIES(water_surface glfw3)
include_directories(
    ${CMAKE_SOURCE_DIR}/libraries/GLFW/include
)


include_directories(
    ${CMAKE_SOURCE_DIR}/libraries/GLM/glm
)
Gweddry
  • 159
  • 8
  • Have you checked that question: https://stackoverflow.com/questions/51564251/correct-way-to-use-third-party-libraries-in-cmake-project? Either use `add_subdirectory()` for enter the library's CMake project or use `ExternalProject_Add()`, as explained in the answer for the question. – Tsyvarev Dec 25 '18 at 15:24
  • Is it not possible to simply add a path to the .lib files and the include folder, like when linking visual studio? I plan to use VS 2013 as a compiler anyway, so all the cmake would do would be setting up the solution parameters, or at least that is my (admittedly poor) understanding of it. The answer to the question you mentioned utilises some .cmake files, which I simply do not have. Is it really not possible to simply tell cmake to link the files I already have, which were fully sufficient for Visual Studio already? – Gweddry Dec 25 '18 at 15:52
  • 1
    Oh, so you want to link with *ready-made* library. Then [that question](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) is for you. (My previous comment is about building the external library from sources within your project.) – Tsyvarev Dec 25 '18 at 16:04
  • Thank you, that did indeed help, only I ran into another problem, which is "error LNK1104: cannot open file 'glew32.lib'" (same error goes for glfw3.lib). I will add my CMakeLists.txt to my question. – Gweddry Dec 25 '18 at 17:16
  • Do you have these `.lib` files? (Exactly `.lib`, not `.dll`). – Tsyvarev Dec 25 '18 at 17:27
  • Of course. As I said, I can compile the code just fine in visual studio, using these files. Upon closer inspection, however, I find that the "link_directories" command did not actually put the path to the .lib files into the solution, which would explain why it can't find the .lib files – Gweddry Dec 25 '18 at 17:50
  • 1
    I'm pretty sure that `link_directories` only effects targets that are created afterwords. In this case the target is created first with `ADD_EXECUTABLE` and then you adjust the `link_directories`. Anyways as shown in the `link_directories` command it is better to simply just put the entire path of the library you want to use. In this case it would be `TARGET_LINK_LIBRARIES(water_surface ${CMAKE_SOURCE_DIR}/libraries/glew-2.0.0/lib/release/Win32/glew32.lib)` – fdk1342 Dec 25 '18 at 18:06
  • @Fred You are right, swapping the commands so that link_directories come before the executable creation did work! The libraries are linked! Thank you! The last issue seems to be that the .dll files are missing. How do I handle that with cmake? (I have the .dll files on hand, I just do not know how to tell cmake where they are) – Gweddry Dec 25 '18 at 18:20
  • 1
    Cmake doesn't need `.dll` files - linkage is performed using `.lib` ones. But you need `.dll` files for run the executable you created. And these `.dll` files are needed to be located near the your executable (in the same directory). There are a number of questions (including Stack Overflow ones) about proper placing `.dll` files with CMake, just search for them. – Tsyvarev Dec 25 '18 at 19:26
  • 1
    @Gweddry In your case I'd suggest to copy the `.dll` files into the binary folder that contains the `.exe`. See https://stackoverflow.com/questions/34799916/copy-file-from-source-directory-to-binary-directory-using-cmake for advice and options on copying a file and https://stackoverflow.com/questions/6594796/how-do-i-make-cmake-output-into-a-bin-dir on how to get the `.exe` into a known location (instead of default debug and release folders). – fdk1342 Dec 25 '18 at 19:55
  • I used configure_file command to copy the .dlls to the build directory and now it works. Thank you for your assistance. – Gweddry Dec 25 '18 at 21:01

0 Answers0