Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding compiled libraries and include files to a CMake Project?

Tags:

c++

gcc

cmake

What is the best method to include a prebuilt library to a cmake project? I want to include FreeType into the project I am working on and the file structure is like this:

  • Build
    • MacOS
      • Make/
      • XCode/
    • Windows
      • VisualStudio/
  • Source
    • libs
      • MacOS
        • libfreetype
      • Windows
        • freetype.dll
    • includes
      • freetype/ (Various header files that are included automatically by ftbuild.h)
      • ftbuild.h (this is what is included in code from my understanding.)
    • MyProject
      • main.cpp
      • foo.cpp
      • foo.h

The library is already compiled. MyProject is the name of the current project.

Thanks! Mike

like image 616
Mike Avatar asked Apr 08 '10 16:04

Mike


People also ask

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.


1 Answers

Just use target_link_libraries with the full path to the prebuilt lib.

So, something like:

# In the file Source/MyProject/CMakeLists.txt
add_executable(my_exe main.cpp foo.cpp foo.h)
if(WIN32)
  target_link_libraries(my_exe ${CMAKE_CURRENT_SOURCE_DIR}/../libs/Windows/freetype.lib)
endif()
if(APPLE)
  target_link_libraries(my_exe ${CMAKE_CURRENT_SOURCE_DIR}/../libs/MacOS/libfreetype.a)
endif()
like image 55
DLRdave Avatar answered Oct 13 '22 01:10

DLRdave