How to correctly add path to libfiles? - cmake

My project has a dependency library in ~/SimGrid/lib directory.
How can I correctly add it to my CMakeLists.txt?
Now I try link_directories(/home/whoami/SimGrid/lib), but it doesn't help and gives:
[ 5%] Linking CXX executable CSim2Sim
/usr/bin/ld: cannot find -lsimgrid
collect2: error: ld returned 1 exit status
My full CMakeLists.txt is here:
project(CSim2Sim)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0")
set(SOURCE_FILES src/main.cpp)
add_executable(CSim2Sim ${SOURCE_FILES})
include_directories(/home/whoami/SimGrid/include)
link_directories(/home/whoami/SimGrid/lib)
target_link_libraries(CSim2Sim simgrid)

On Linux, linker expects library filename to be prefixed with "lib":
/home/whoami/SimGrid/lib/libsimgrid.so
If your library file has no such prefix, it cannot be found by the linker using plain library name.
As opposite, specifying full library filename in target_link_libraries call works always:
# Assume filename of the library is 'simgrid.so'
target_link_libraries(CSim2Sim /home/whoami/SimGrid/lib/simgrid.so)
In that case link_directories() call isn't needed.

link_directories only tells CMake what directories to look inside for the libraries you want to link to, not what libraries you want to link. My suggestion would be to create a file named SimGrid.cmake and add the following:
# -*- cmake -*-
set(SIMGRID_INCLUDE_DIRS
${CMAKE_SOURCE_DIR}/SimGrid/include
)
set(SIMGRID_LIBRARIES
simgrid.a
simgrid2.a # etc for the names of the actual libs you want to link
)
In your CMakeLists.txt add include(SimGrid) to pull the new file in.
Then in your CmakeLists.txt use the new variables and target_link_libraries which is the function you use to tell the linker what libraries you want to link:
include_directories(${SIMGRID_INCLUDE_DIRS})
target_link_libraries(CSim2Sim ${SIMGRID_LIBRARIES})
Of course, you could just add it all to your single CMakeLists.txt, but dividing it up is good practice to keep things manageable.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0")
set(SOURCE_FILES src/main.cpp)
add_executable(CSim2Sim ${SOURCE_FILES})
link_directories(${CMAKE_SOURCE_DIR}/SimGrid/lib)
include_directories(${CMAKE_SOURCE_DIR}/SimGrid/include)
set(SIMGRID_LIBRARIES
simgrid.a
simgrid2.a
)
target_link_libraries(CSim2Sim ${SIMGRID_LIBRARIES})

My recommendation is to find the libs your project depends on and link it to the target. Thus any absolut or relative path are not in the cmake file but detected while run of cmake.
See How can I find a library name of .so file?

Related

About Specifying EIGEN3_DIR variable not use

My computer has multiple versions of the eigen library. I have an eigen3 in the /usr/include/ and another eigen3 in the /usr/local/. I use the set() command in cmakelists, but the compiler only uses the eigen library in the/usr/include directory. Here are my cmakelists.txt settings
`
cmake_minimum_required(VERSION 2.8.3)
project(cmake_test)
set(EIGEN3_DIR "/usr/local/eigen-3.3.9/share/eigen3/cmake/")
find_package(Eigen3)
include_directories(
# "/usr/local/eigen-3.3.9/include/eigen3/"
${PROJECT_SOURCE_DIR}/include
${EIGEN3_INCLUDE_DIR}
)
message("EIGEN3_INCLUDE_DIR =======" ${EIGEN3_DIR})
message("EIGEN3_INCLUDE_DIR =======" ${EIGEN3_INCLUDE_DIR})
add_executable(cmake_test src/main.cpp)
`
Does anyone know where I went wrong
I hope someone can tell me how cmake searches for header files, library files, and Search order of the find_ package() command

CMake library not found

I try to run the makefile that using cmake produced. It generate an error
ld: library not found for -lhello
clang: error: linker command failed with exit code 1 (use -v to see invocation)
the file directory is:
the cmakelists.txt is:
the main.c file is:
the ERROR:
I think I set the right directory. How to solve this ERROR?
CMake has a system if you want to link libraries. For many standard libraries we have cmake modules which will allow you to use the find_package command. This will set some variables for include directories and libraries. If there is no such thing for your library you can use find_path for the include files and find_library to search for a library.
Here is what you could do (untested, just out of my head):
add_executable(main main.c)
target_include_directories(
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_SOURCE_DIR}/include/hello
)
find_library (
HELLO_LIB
NAMES hello libhello # what to look for
HINTS "${CMAKE_SOURCE_DIR}/lib" # where to look
NO_DEFAULT_PATH # do not search system default paths
)
# check if we found the library
message(STATUS "HELLO_LIB: [${HELLO_LIB}]")
if (NOT HELLO_LIB)
message(SEND_ERROR "Did not find lib hello")
endif
target_link_libraries(main
${HELLO_LIB}
)
Use message to debug your cmake files. If you define the library in cmake as well you can link directly against the cmake target.
When your library isn't in a standard path liking /usr/lib, you should use link_directories() in your CMakeLists.txt to specify a non-standard library path which contains your library. Notice that you MUST put your link_directories() ahead of add_executable() as the following shows:
link_directories(../../lib)
add_executable(newhello main.c)
include_directories(../../include)
target_link_libraries(newhello hello)

Ogre Error In Cmake

While implementing CMake in my code I am getting an ogre error:
/usr/bin/ld: cannot find -lOGRE
My CMakeLists.txt file looks like:
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)
#Name your project here
project(eCAD)
#sets cmake to run moc when needed
set(CMAKE_AUTOMOC ON)
#find requirements of this projects
find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(OGRE)
find_package(OIS)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#Sends the -std=c++11 flag to the gcc compiler
add_definitions(-std=c++11)
qt5_wrap_ui(Cmake_form_hdr resources/ui/mainwindow.ui)
#This tells CMake to main.cpp and name it eCAD
add_executable(eCAD main.cpp ${Cmake_form_hdr})
#include the subdirectory containing our libs
add_subdirectory (gui)
include_directories(gui)
#link_libraries
target_link_libraries(eCAD Qt5::Widgets Qt5::Core OGRE OIS)
I am new to this. Please help me out to solve the problem
The command find_package(OGRE) runs file FindOGRE.cmake and sets variables OGRE_INCLUDE_DIRS and OGRE_LIBRARIES. To link with the OGRE library, you should use these variables, e.g.
target_include_directories(eCAD PRIVATE ${OGRE_INCLUDE_DIRS})
target_link_libraries(eCAD ${OGRE_LIBRARIES})
This is same for all of the external libraries you use.

Getting a CMake Error: Cannot specify link libraries for target which is not built by the project

I am implementing CMake in my code but I'm getting the error
"Cannot specify link libraries for target "Qt5::Widgets" which is not built by the project".
Below are the contents of the CMakeLists.txt:
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)
#Name your project here
project(eCAD)
#Sends the -std=c++11 flag to the gcc compiler
ADD_DEFINITIONS(-std=c++11)
#This tells CMake to main.cpp and name it eCAD
add_executable(eCAD main.cpp)
#include the subdirectory containing our libs
add_subdirectory (gui)
include_directories(gui)
#include Qt directories
find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(Qt5Designer)
SET(QT_USE_QTDESIGNER ON)
#link_libraries
target_link_libraries(Qt5::Widgets Qt5::Core)
In addition to the accepted answer: An important detail is to place target_link_libraries after the add_executable and find_package lines, so all linked components are known.
The first argument of target_link_libraries is the target name:
target_link_libraries(eCAD Qt5::Widgets Qt5::Core)
Also, do not confuse target name with the project name:
a command project specifies a project name, but
a target is the one created with add_executable, add_library or add_custom_target.
The error message is about the target.
Set you_lib_name before setting target_link_libraries
set(you_lib_name libname)
target_link_libraries(you_lib_name Qt5::Widgets Qt5::Core)

CMake adding libraries for Windows/Linux

Visual Studio C++ 2008 / GCC 4.4.2
I have written a program to run on Linux and now I have to port my code to run on Windows. I have decided to use CMake as I want to keep the same build system for both platforms.
However, I need to link with some libraries for both platforms. In my CMakeLists.txt I have the following:
# Compile with gcc c89 standard
IF(CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(STATUS "GCC detected - Adding compiler flags")
SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)
SET(CMAKE_C_FLAGS "ws2_32.lib")
ENDIF(WIN32)
However, when I compile on Visual Studio I get the following error:
fatal error C1083: Cannot open source file: 'ws2_32.lib': No such file or directory
What can I do to resolve this problem?
========= Edit
In the top level directory
# Project Client Server
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
# Name of project
PROJECT(CLIENT_SERVER)
# Compile with gcc c89 standard
IF(CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(STATUS "GCC detected - Adding compiler flags")
SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)
SET(CMAKE_C_FLAGS "ws2_32")
ENDIF(WIN32)
# Includes
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/cltsvr_ults)
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/server)
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/client)
# libraries
LINK_DIRECTORIES($CLIENT_SERVER/cltsvr_ults)
# Add subdirectories
ADD_SUBDIRECTORY(client)
ADD_SUBDIRECTORY(server)
ADD_SUBDIRECTORY(cltsvr_ults)
ADD_SUBDIRECTORY(test_client)
ADD_SUBDIRECTORY(test_server)
In the subdirectory of client I have this CMakeLists.txt
# libray called client from client.c
ADD_LIBRARY(client client)
And in the subdirectory of test_clt where I create and link my executable.
# Test client add executable
INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/client)
INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/cltsvr_ults)
# Link the library
LINK_DIRECTORIES($CLIENT_SERVER/client)
# Add the executable
ADD_EXECUTABLE(clt test_clt)
# Link the executable to the client library
IF(WIN32)
TARGET_LINK_LIBRARIES(clt client ws2_32)
ENDIF(WIN32)
Disclaimer: My answer is of philosophical nature which should encourage you to avoid touching CMAKE_C_FLAGS directly. For the direct answer that just solves your problem look what Bill ( the lead architect of the CMake btw. ) wrote.
The thing about CMake is, that it lets you describe what you want to do without referring to a specific compiler or platform. What CMake does is building the compiler and linker flags from your usage of
include_directories
add_definitions
add_library
add_executable
target_link_libraries
If there are no external dependencies, other than the compiler itself, this is all you need. For external dependencies use
find_package
It defines a set of variables, like
find_package(SDL)
defines
SDL_INCLUDE_DIR
SDL_LIBRARY
for usage with respectively include_directories and target_link_libraries. CMake ships with a bunch of so called module files, like FindSDL.cmake and many others can be googled.
The next lower level is to use
find_path
find_library
which are used in the Find???.cmake modules itself.
The CMAKE_C_FLAGS variable is composed by CMake from these commands. Modifying it means you bypass CMake. There are cases, like for special optimization flags, you want to do this, but at this point all power and thus responsibility transfered from CMake to you.
By adding ws2_32.lib to the C_FLAGS, you are using it at compile time, and not link time. If you look at the error message, you can see it it treating the file as if it were a c++ source file: Cannot open source file: 'ws2_32.lib'. target_link_libraries(target ws2_32) should work.
You need to use the Target Link Libraries command. The target would be the executable you're building.
EDIT: You shouldn't specify the libs you're linking against in C_FLAGS. You can do something like TARGET_LINK_LIBRARIES(execName, ws_32, ...). I'm not 100% sure if you need the .lib. Been a while since I used CMake.