CMake: how to specify from command line where to look for locally installed libraries - c

I am building locally a dependencies and installing in a local directory. Now I would like to tell CMake to look into that local directory for include and libraries, in addition to all the standard places.
I tried this:
cmake -D CMAKE_LIBRARY_PATH=`realpath ../target`/lib CMAKE_INCLUDE_PATH=`realpath ../target`/include .
But it did not work. Any idea?

You can use the
find_library()
command to search for libraries.
With
include_directories()
you tell cmake where to look for include files

You can first add the folder to your PC's system environment, e.g. called PersonalLib_DIR that points to the folder. Then you can add it for include and libraries by accessing $ENV{PersonalLib_DIR} via CMake:
For include:
include_directories($ENV{PersonalLib_DIR})
For libraries (assume you want to link the aLib.lib under the folder):
target_link_libraries(youProject $ENV{PersonalLib_DIR}/aLib)

Related

How do I ask CMake to copy include directory while running "cmake install"?

Previously, I'm looking for this kind of question but none of them are working as example this thread and this one.
Currently, I'm creating a C library which supposed to outputs a shared library (*.so or *.dll) file and multiple *.h files in a ./include directory. The shared library is successfully built and installed to /usr/local/lib. However, I have no idea how do I tell CMake to copy *.h files to the correct destination as below.
In POSIX: /usr/local/include
In Windows: C:\<Compiler Path>\include
I know I could workaround this by copying those *.h with Python script or using if-else logic on CMake. But at least tell me if there is a feature in CMake for doing that thing. Thanks!
Here's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project (
learn_c_library
VERSION 1.0.0
LANGUAGES C
)
# Specify CMake build and output directory
set(CMAKE_BINARY_DIR "${PROJECT_SOURCE_DIR}/.cmake")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/out")
# Use every .c file inside ./src
file(GLOB_RECURSE MY_SOURCE_PATH "src/*")
add_library(
${PROJECT_NAME} SHARED
${MY_SOURCE_PATH}
)
include_directories(
${PROJECT_NAME}
"include/"
)
# Allows "make install" to install shared library
install(TARGETS ${PROJECT_NAME})
This answer improves on your answer:
cmake_minimum_required(VERSION 3.14)
# ...
include(GNUInstallDirs)
install(DIRECTORY "include/"
TYPE INCLUDE
COMPONENT MyProj_Development)
The GNUInstallDirs module provides standard configuration points for customizing a project's install layout. These variables should essentially always be used instead of hardcoding path/directory names.
The TYPE INCLUDE argument to install() was introduced in CMake 3.14 and uses GNUInstallDirs's CMAKE_INSTALL_INCLUDEDIR variable to determine the destination.
Finally, and especially if your project is a library, all your install() rules should be given a COMPONENT argument to allow splitting files between runtime and development packages (and potentially others like documentation and architecture-independent data files).
After days of fiddling CMake, finally I found the solution!
Let's assume you store your public *.h inside ./include directory, then here's the syntax for installing your *.h files to your machine:
install(
DIRECTORY
"include/" # Your global headers location in the project
DESTINATION
include # This is your system's default "include" location
)
I got the solution from here with usr/myproject replaced to be include

How to install C source files and headers?

I've been given these source files and headers. In the README.md the authors explain how to launch the test executables without the need of a proper installation. It is just a make command to run. They explain how to generate the .so files. I think these latter are meant to be used if I wanted to install the APIs at a system level (the definitions should be in api.h). My question is: where should I copy the shared objects generated by the Makefile and the api.h header? I aim to write a source file from scratch where I use those APIs (e.g. crypto_sign()) just including the headers, if it is possible. Thanks
where should I copy the shared objects generated by the Makefile and the api.h header? I aim to write a source file from scratch where I use those APIs (e.g. crypto_sign()) just including the headers, if it is possible
Nowhere.
The project comes with CMake support. Use CMake in your project and just add_subdirectory the repository directory.
Anyway, if you really wish to install the library system-wide, then FHS specifies directory structure on linux. For local system administration use /usr/local/lib for local libraries .so files and /usr/local/include for local C header files.

How can I change the default search paths in GCC?

We have one build machine which has some static libraries at unusual paths, and gcc doesn't find them when via the -l option, all other build machines run fine. But it seems this one configured incorrectly or something.
The solutions we have tried:
Check the host name of the build machine in build script and add the -L command line option if it matches name the problematic build machine (very ugly).
Print the list of search dirs using the -print-search-dirs option
and symlink the problematic library into the first one (too hackish).
What I would like to do is simply add an extra path to search paths system wide to the gcc.
Is there a way to change/configure the default library search paths in GCC? Is there a config file where the list of the defaults are stored?
You can use
-l:<PATH_TO_YOUR_LIBRARY>
Or you can set up the /etc/ld.so.conf with the path to your directory of which you have the shared library installed. You might have to run ldconfig after that.

Creating CMake project for libwebsocket

The title states the problem statement: I'm trying to create a CMake project utilizing the libwebsocket library, but I can't find any information for doing so.
Is there anyone who have tried this? A simple CMakeLists.txt for e.g. the test-server program would be much appreciated!
I've compiled and installed the library on my Ubuntu 14.04 machine.
EDIT: I would also like to know if anyone has experience in using the libwebsocket lib w/ C++?
EDIT 2:
After using #evadeflow's answer I'm able to run cmake and build the project. However now I get the following runtime error:
And here's an ls of the /usr/local/lib/ folder
It seems like the libwebsockets.so.7 file is not found?
From CMake:
${LIB_WEBSOCKETS_INCLUDE_DIRS} = /usr/local/lib
${LIB_WEBSOCKETS_INSTALL_DIR} = /usr/local
EDIT 3:
Solved edit 2 by:
Editing the file /etc/ld.so.conf and add /usr/local/lib.
Reference: https://lonesysadmin.net/2013/02/22/error-while-loading-shared-libraries-cannot-open-shared-object-file/
If you've already installed libwebsockets, something like this ought to work:
cmake_minimum_required(VERSION 2.8)
find_package(PkgConfig)
pkg_check_modules(LIB_WEBSOCKETS REQUIRED libwebsockets)
get_filename_component(
LIB_WEBSOCKETS_INSTALL_DIR
${LIB_WEBSOCKETS_LIBRARY_DIRS}
DIRECTORY
)
add_executable(
test-server
test-server/test-server.c
test-server/test-server-http.c
test-server/test-server-dumb-increment.c
test-server/test-server-mirror.c
test-server/test-server-status.c
test-server/test-server-echogen.c
)
target_link_libraries(
test-server
${LIB_WEBSOCKETS_LIBRARIES}
)
set_target_properties(
test-server
PROPERTIES
INCLUDE_DIRECTORIES
${LIB_WEBSOCKETS_INCLUDE_DIRS}
LINK_FLAGS
"-L${LIB_WEBSOCKETS_LIBRARY_DIRS}"
COMPILE_DEFINITIONS
INSTALL_DATADIR="${LIB_WEBSOCKETS_INSTALL_DIR}/share"
)
This is basically a stripped-down version of what's in the CMakeLists.txt file from the libwebsockets github project, without all the platform- and build-specific conditionals.
I hope this is enough to satisfy your request for a 'simple' CMakeLists.txt example. I tested it with CMake version 2.8.12.2; it should work fine as-is if you've installed libwebsockets to its default prefix of /usr/local; however, if you installed to a different location, you will need to set PKG_CONFIG_PATH in the environment from which you invoke cmake.
Also, as explained in the CMake documentation, you will need to replace DIRECTORY with PATH in the get_filename_component() invocation if you're using CMake 2.8.11 or earlier.
UPDATE: Regarding the file not found error from your follow-up comment, this is almost certainly due to libwebsocket.so[.7] not being on the linker's default path. There are at least three ways to fix this, but the easiest way to verify that this is the problem would be to just launch the app from the terminal using:
$ LD_LIBRARY_PATH=/usr/local/lib ./test-server
If it works, you know that was the issue. (Oops—I see you've figured it out in the meantime. Yeah, updating /etc/ld.so.conf is another way. Or, you can force CMake to link to the static version of libwebsockets [as described in this answer] is another. But I like your solution best.)
UPDATE: One thing that wasn't mentioned about /etc/ld.so.conf is that you generally need to run sudo /sbin/ldconfig after editing it in order to update the shared library cache. And—when setting non-default paths for a particular application—many people consider it good form to add a new 'sub-config file' in /etc/ld.so.conf.d rather than edit the global ldconfig file. (For the case of adding /usr/local/lib, though, this is such a common requirement I'd be inclined to dump it in the global config, which is what lots of Linux distros do, anyway.)

Add one header from a different project to my project using automake

I'm working on a relatively big project that is using automake build system.
Now the problem is that I need to link the project with a library from another project (this works fine), but I also need to include a header from the other project source tree (api.h).
INCLUDES = -I#REMOTE_PROJECT_DIR#
in Makefile.am doesn't work, because there are .h files with coliding names in the remote source directory. How can I add just the api.h?
I used a symlink into the include directory in the project, but now I need to push the sources into a public repo and everyone working with it has the other project in a different directory, so I need to use the configure param.
You do not want to tweak you Makefile.am or your configure.ac in any way. If api.h is installed in a standard location (eg /usr/include), then all you need is AC_CHECK_HEADERS([api.h]) in configure.ac. If api.h is installed in a non-standard location (eg /home/joe/include), the way to pick it up in your project is determined at configure time. When you run configure, you add the argument CPPFLAGS=-I/home/joe/include to the invocation of configure. You do not indicate the non-standard location in the build files themselves.
Another alternative is to use pkg-config, but the non-standard location of your header file will still be dealt with when you run configure. (This time by setting PKG_CONFIG_PATH rather than CPPFLAGS)
If you have headers with same names, you could put at least one of them into directory with different name and include it using directory name.
Here's a sample directory structure:
mylibrary/include/myblirary/api.h
myproject/api.h
myproject/main.cpp
In main.cpp:
#include "api.h"
#include "mylibrary/api.h"
#include <boost/regex.hpp>
When compiling:
g++ -I mylibrary/include

Resources