I have a simple problem with the linking of libraries with CMake (I don't know CMake very well).
My configuration is the following :
project/src/CMakeLists.txt (with all .cpp and .h files)
project/support/linux/gmp/include/gmp.h
project/support/linux/gmp/include/gmpxx.h
project/support/linux/gmp/include/libgmp.a
project/support/linux/gmp/include/libgmpxx.a
How to include the library gmp in the process of compilation ? (I am lost between FIND_PACKAGE, INCLUDE_DIRECTORIES, TARGET_LINK_LIBRARIES, ADD_LIBRARY ... commands)
Thank you very much.
CMake is not so hard to understand.
First Step
Use find_package to locate the libary.
find_package(GMP REQUIRED)
Second step
Use include_directories to include the libary header files.
include_directories(${GOBJECT_INCLUDE_DIR})
Third Step
Use target_link_libraries to link your binary against the libary.
add_executable(ExecutableName Main.cpp)
target_link_libraries(ExecutableName ${GOBJECT_LIBRARIES})
Related
I'm making a CMake project on MacOS, and my project requires libraylib.a and multiple different frameworks. This is what I have:
cmake_minimum_required(VERSION 3.10)
project(RaylibHW)
# Adding executable
add_executable(rlhw my_app.c)
#linking C archives
# Linking frameworks
if(APPLE)
add_library(libraylib STATIC libraylib.a)
target_link_libraries(rlhw PRIVATE
"-framework CoreVideo"
"-framework IOKit"
"-framework Cocoa"
"-framework GLUT"
"-framework OpenGL"
)
endif()
I can link either the frameworks or the archive file, but since apparently it's doesn't work to use target_link_libraries more than once, I can't seem to do it.
I want to use functions in the header files gmp.h and mpfr.h, which are in the file /opt/local/include.
But when I run gcc with -v, all of the search paths are something like /Application/Xcode.app/Contents/etc.
I have tried adding LD_LIBRARY_PATH="/opt/local/include" to .bash_profile but it doesn't work. The compiler either tells me that 'gmp.h' file not found, or Undefined symbols for architecture x86_64.
What should I do?
Converting comments into an answer.
You need to add -I/opt/local/include to compile commands (to specify where the headers are) and -L/opt/local/lib and -lgmp and -lmpfr (possibly in the reverse order — MPFR before GMP) to link commands.
That works! Would you mind explaining a little bit the logic behind this? For example if I had another header file header.h I need, how should I include it?
You include it with #include "header.h". You compile the code with -I/directory/containing/header to find the header. You specify where the library (libheader.a or libheader.dylib, since you seem to be on macOS) is too, with -L/directory/containing/lib and -lheader — or whatever is appropriate.
The -I tells the preprocessor to look in the named directory for header files, so it looks for /directory/containing/header/header.h, for example.
The -L tells the linker where to find libraries (so it looks for /directory/containing/lib/libheader.dylib etc).
The -lheader tells the linker to look for libheader.a or libheader.dylib (or local equivalents) for the libraries.
Except for the use of .dylib vs .so vs .dll vs … (and .a vs .lib vs …), the same principles apply to other systems too.
This is probably a duplicate.
I want to use the GBExtended library in my game but I am having difficulty understanding how I would include the library.
I can see that the GBExtended library (at least from the loderunner example source) contains the directories includes/, lib/ and src/ and the file which I need to include is lib/gbext.lib.
I would assume that you would have to include the library in your files as needed:
#include <gbextended/screen.h>
# etc ...
What flags do you have to pass to the lcc compiler so that it will include these libraries? I'm having trouble identifying this from the examples.
With SDCC you can use the -l flag (lowercase L) to specify a library to be included, and the -L flag (uppercase L) to specify the directory from which the files can be found.
You also have to use -I (uppercase I) to specify where the header files are located.
First of all, i'm just a newbie in a CMake magic. And i just want to link libgit2 to my simple C program in CMake way (FindLibgit2.cmake).
As i understand from cmake documentation my CMakeLists.txt should looks like that:
project(libgit2test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# This variables should be setting up externally, i know - i show them just for example
set (LIBGIT2_LIBRARIES "<path to directory with git2.lib and git2.dll")
set (LIBGIT2_INCLUDE_DIR "<path to libgit2/include>")
find_package(Libgit2 REQUIRED)
include_directories(${LIBGIT2_INCLUDE_DIR})
set(LIBS ${LIBS} ${LIBGIT2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${LIBS})
In my simple program i just call a couple of simple libgit2 functions and get this:
WARNING: Target "libgit2test" requests linking to directory "<libgit2 build directory>". Targets may link only to libraries. CMake is dropping the item.
I think the problem is here: target_link_libraries(${PROJECT_NAME} {$LIBS})
I try to change it to git2, but than i just get can not open file.
What i'm doing wrong?
P.S. I'm using Visual Studio 2010 compiler, Qt Creator to create CMake project, and successfully build libgit2 with CMake.
Remove
set (LIBGIT2_LIBRARIES "<path to directory with git2.lib and git2.dll")
set (LIBGIT2_INCLUDE_DIR "<path to libgit2/include>")
These variables should be set by find_package(Libgit2 REQUIRED).
I've found the source of problem: variable LIBGIT2_LIBRARIES must point to lib file itself, not the directory of it (debug/release).
However, setting up manually this variables looks like wired. I want to find more "automatic" way to find libgit2 - if one exists.
I quite new with CMake, but I have a problem with porting an existing library to it.
In order to simplify, I will only work on 2 files : angle.cpp and angle.h.
The files are this ones :
/cmaketest/CMakeLists
/cmaketest/src/angle.cpp
/cmaketest/src/angle.h
and I will run CMake and produce the Makefile in /cmaketest/.
My CMakeLists is currently this one :
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(cmaketest)
SET(cmaketest_SRCDIR src)
AUX_SOURCE_DIRECTORY(${cmaketest_SRCDIR} cmaketest_SOURCES)
FILE(GLOB cmaketest_HEADERS ${cmaketest_SRCDIR}/*.h )
ADD_EXECUTABLE(cmaketest ${cmaketest_SOURCES} ${cmaketest_HEADERS})
But the problem is that in angle.cpp, the header is included not by "angle.h" but by <src/angle.h>
So with the current cmake file I got the following error when I execute make :
/cmaketest/src/angle.cpp:1:23: fatal error: src/angle.h: file not found
How to solve the problem ? (for backward compatibility I can't modify <src/angle.h> in the .cpp file)
Thank you very much.
Try adding the project directory as an include directory by using the include_directories command:
...
file (GLOB cmaketest_HEADERS ${cmaketest_SRCDIR}/*.h )
include_directories(${CMAKE_SOURCE_DIR})
add_executable(cmaketest ${cmaketest_SOURCES} ${cmaketest_HEADERS})