I'm configuring my project for CMake and am having linking problems - the project files all compile successfully, then it says it is linking and reports all sorts of symbols not found.
These symbols are mostly provided by my own code, while some of them are provided by BerkeleyDB, which is being properly located and included.
Here is my top-level CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project( rpdb C )
# add local modules path for project
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/" )
# where to look for source files (headers and source)
include_directories( include src )
# define sub-directories of source that cmake knows about as well a where their output will be put
add_subdirectory( src bin )
# compiler-dependent flags:
if( CMAKE_COMPILER_IS_GNUCC )
# gcc
add_definitions( -ggdb -fsigned-char -freg-struct-return -Wall -W -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Winline -Werror )
else( CMAKE_COMPILER_IS_GNUCC )
# non-gcc (intended for clang)
add_definitions( -ggdb -fsigned-char -Wall -W -Wshadow -Wstrict-prototypes -Wpointer-arith -Wcast-qual -Winline -Werror )
endif( CMAKE_COMPILER_IS_GNUCC )
# distribution configuration
set(CMAKE_C_FLAGS_DISTRIBUTION "-O3")
set(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3")
And here is my src-level CMakeLists.txt:
# make sure we have libdb
find_package( BerkeleyDB REQUIRED )
include_directories( ${libdb_INCLUDE_DIRS} )
target_link_libraries( rpdb ${libdb_LIBRARIES} )
# define variable specifying included source files - all .c files below this directory
file( GLOB rpdb_src "**/*.c" )
# define shared library with sources
add_library( rpdb SHARED ${rpdb_src} )
The output (partial):
...
[100%] Building C object bin/CMakeFiles/rpdb.dir/RPDB_TransactionController/RPDB_TransactionController.c.o
Linking C shared library librpdb.dylib
Undefined symbols:
"_RPDB_ReplicationVerbositySettingsController_displayMessageProcessingInformation", referenced from:
_RPDB_SettingsController_internal_setVerbosity in RPDB_SettingsController.c.o
...
All of the symbols do actually exist. The result seems to occur for symbols in object files other than the one it is currently looking at.
The output from "cmake ../" (from install, a directory in the top-level):
=> cmake ..
-- Found BerkeleyDB: /usr/local/lib/libdb.dylib
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ahaig/Projects/RPDB/RPDB_C/install
Any help much appreciated.
Instead of GLOB, try GLOB_RECURSE (CMake 2.8.3 docs, file command), which you can use like this: file ( GLOB_RECURSE rpdb_src "*.c" )
An example:
function (add_test_files)
set ( src_files "" )
####################################################
# Find all C/C++ files recursively from current dir
####################################################
foreach ( ext IN ITEMS "cpp" "cxx" "cc" "c" )
file ( GLOB_RECURSE _files "*.${ext}" )
set ( src_files ${src_files} ${_files} )
endforeach ()
message ( STATUS "Found: ${src_files}" )
add_executable ( test ${src_files} )
endfunction ()
add_test_files ()
My problem came down to an issue with file inclusion. I was including files via glob using **/*.c, which was apparently being treated as */*.c. Perhaps I'm misunderstanding glob patterns here - I have only used them minimally otherwise in Ruby's file globbing.
In any case, here is the solution I came up with:
# function processes each sub-directory and then adds each source file in directory
# each function should cascade back upward in setting variables so that the bottom directory
# adds its source first, and then the level above it adds its source and passes both up, and so on...
function( recursively_include_source which_directory )
# get list of source from this directory
file( GLOB this_directory_src "${which_directory}/*.c" )
# get list of all files for this directory
file( GLOB this_directory_all_files "${which_directory}/*" )
if( this_directory_all_files AND this_directory_src )
# remove source from list of files to get list of directories
list( REMOVE_ITEM this_directory_all_files ${this_directory_src} )
set( this_directory_directories ${this_directory_all_files} )
# for each sub-directory, call self with sub-directory as arg
foreach( this_sub_directory ${this_directory_directories} )
recursively_include_source( ${this_sub_directory} )
endforeach( this_sub_directory ${this_directory_directories} )
endif( this_directory_all_files AND this_directory_src )
# add source files to ${rpdb_src} in PARENT_SCOPE
set( rpdb_src ${rpdb_src} ${this_directory_src} PARENT_SCOPE )
endfunction( recursively_include_source which_directory )
This includes all .c files in and below the current directory.
I call the function like this:
recursively_include_source( ${CMAKE_CURRENT_SOURCE_DIR} )
So the final src-dir CMakeLists.txt looks like this:
# make sure we have libdb
find_package( BerkeleyDB REQUIRED )
include_directories( ${DB_INCLUDE_DIRS} )
set(LIBS ${LIBS} ${DB_LIBRARIES})
recursively_include_source( ${CMAKE_CURRENT_SOURCE_DIR} )
# define shared library with sources
add_library( rpdb SHARED ${rpdb_src} )
target_link_libraries( rpdb ${LIBS} )
And everything seems to be working.
make VERBOSE=1
Also, look at CMakeCache.txt
Related
I have a CMake script where the final executable is linked with my own linker script:
cmake_minimum_required(VERSION 3.1)
project(test_app)
set(LINKER_SCRIPT "linker.ld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${LINKER_SCRIPT}")
add_executable(${PROJECT_NAME}.elf
main.cpp
startup.cpp
)
How do I make an executable dependent also on the linker script file (trigger linking if linker.ld was changed)?
You can add a LINK_DEPENDS property to your executable target, using set_target_properties. Add the following line after your add_executable command:
set_target_properties(${TARGET_NAME} PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})
The first argument to set_target_properties is the target name, i.e. the first argument you passed to add_executable.
I found this mail which described three possible ways for forcing an executable to be dependent on a linker script. Its author prefers this way:
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(LINKERSCRIPT C)
FILE(WRITE main.c "void main(){}")
# dummy.c must exist:
ADD_EXECUTABLE(EXE main.c dummy.c)
# linkerscript must exist:
SET_SOURCE_FILES_PROPERTIES(
dummy.c PROPERTIES OBJECT_DEPENDS ${CMAKE_SOURCE_DIR}/linkerscript
)
Here dummy.c is an empty file, which is listed for the add_executable() command only for make resulted executable dependent on the linker script via the OBJECT_DEPENDS property.
I need to compile a static library composed by c files and one asm file using CMake. In the lib dir I have a CMakeLists.txt similar to the following:
I have moved the
set(CMAKE_ASM_COMPILER nasm)
in an upper cmake list and modified de inner
enable_language(C ASM)
include(sourcelist)
add_library(myLib ${sources} foo.asm)
set_property( SOURCE ${sources} APPEND PROPERTY
COMPILE_DEFINITIONS "MYDEF1" ;"MYDEF2")
set_source_files_properties(${sources} PROPERTIES COMPILE_FLAGS -fPIC)
set_property(
foo.asm
APPEND
PROPERTY COMPILE_DEFINITIONS
"QNXNTO")
set_source_files_properties(${sources_asm} PROPERTIES COMPILE_FLAGS "-f elf ")
target_include_directories(myLib PRIVATE ../}
where ${sources} are my c files that are compiled correctly, but
when I try to compile the asm file I got
/usr/bin/nasm -DQNXNTO -f elf -o CMakeFiles/mylib.dir/foo.asm.o -c foo.asm
nasm: error: unrecognised option `-c'
So I have the problem with -c option, it should be -s
How can I remove the unwanted "-c" ?
I have solved the issue:
in the outer cmake list set
.....
set(CMAKE_ASM_NASM_COMPILER /usr/bin/nasm)
set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS asm)
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf)
.....
the inner one will be modified in the folowing way:
enable_language(C ASM_NASM)
include(sourcelist)
add_library(myLib ${sources} foo.asm)
set_property( SOURCE ${sources} APPEND PROPERTY
COMPILE_DEFINITIONS "MYDEF1" ;"MYDEF2")
set_source_files_properties(${sources} PROPERTIES COMPILE_FLAGS -fPIC)
set_property(
foo.asm
APPEND
PROPERTY COMPILE_DEFINITIONS
"QNXNTO")
target_include_directories(myLib PRIVATE ../}
The output:
/usr/bin/nasm -DQNXNTO -f elf -o CMakeFiles/mylib.dir/foo.asm.o -c foo.asm
I have a C application which is running on Raspberry Pi 3 and currently, I have to build it on PI with Cmake. I am trying to build it on Ubuntu machine. I have added a CMAKE_TOOLCHAIN_FILE as described here.
I could run cmake. -DCMAKE_TOOLCHAIN_FILE without any problem but the "make" command is not successful and it can not find a header file inside one of the external library: "mirsdrapi-rsp". The error message is:
fatal error: mirsdrapi-rsp.h: No such file or directory
#include "mirsdrapi-rsp.h"
^
compilation terminated.
I have created a folder named "lib" and have put the "libmirsdrapi-rsp.so" file inside it.
my CMakeLists.txt is as below:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra -v -g -D_XOPEN_SOURCE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -v ")
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-v")
set (SDR_API_PATH ${CMAKE_SOURCE_DIR}/lib)
include_directories (include ${SDR_API_PATH})
include_directories("${CMAKE_SOURCE_DIR}/lib")
find_library(mirslocation NAMES mirsdrapi-rsp HINTS ${SDR_API_PATH} NO_CMAKE_FIND_ROOT_PATH)
message(STATUS ${mirslocation})
add_library(mirs STATIC IMPORTED)
set_target_properties(mirs PROPERTIES IMPORTED_LOCATION ${mirslocation})
target_link_libraries (raspberryPiDaemon mirs)
target_link_libraries(raspberryPiDaemon m)
Cmake is printing the right path of the library mirsdrapi-rsp while running "find_library" and as I mentioned I am getting the error message just while running "make" command and not "cmake" command.
My content of CMAKE_TOOLCHAIN_FILE is as below:
# Define our host system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
# Define the cross compiler locations
SET(CMAKE_C_COMPILER ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
# Define the sysroot path for the RaspberryPi distribution in our tools folder
SET(CMAKE_FIND_ROOT_PATH ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/sysroot/SET)
# Use our definitions for compiler tools
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers in the target directories only
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
add_definitions(-Wall -std=c11)
Anybody knows how I can add the header file from mirsdrapi-rsp library to include path?
I guess that you're setting include_directories to the wrong path (it is set 2 times to ${CMAKE_SOURCE_DIR}/lib which must be the folder of libraries not the header files). Check again the correct location of the missing header file.
More precisely: you need to find the path of mirsdrapi-rsp.h and let CMake know it just like for find_library:
find_path(MIRSDRAPI_INCLUDE_DIRS NAMES mirsdrapi-rsp.h PATHS {proper-location})
if (MIRSDRAPI_INCLUDE_DIRS)
target_include_directories(raspberryPiDaemon PRIVATE ${MIRSDRAPI_INCLUDE_DIRS})
endif()
In addition, you can set the INTERFACE_INCLUDE_DIRECTORIES property to the library like this:
set_property(TARGET mirsdrapi-rsp APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${MIRSDRAPI_INCLUDE_DIRS})
This way, target_include_directories might be unnecessary and target_link_directories should be enough.
My source tree is
cpp
├── bin
├── CMakeLists.txt
├── src
├── headers
├── Makefile
├── TestConfig.h
├── TestConfig.h.in
├── README.txt
├── third_party
├── x_build
└── xtoolchain.cmake
I am using crosstool-ng.
Edit: Additional info (that may help others): I used reference of http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/ to boot RPI via QEMU (important resizing the img file to 8GB).
qemu-img resize 2014-01-07-wheezy-raspbian.img +6G
sudo fdisk /dev/mmcblk0
- use "parted" or 'd' options to delete the 2nd partition and then recreate it but with a larger size. (don't worry, the data will remain). 1st partition is vfat.
- reboot to activate the partition changes.
- use "sudo resize2fs /dev/mmclk0p2" (or /dev/sda2 if symlink set in udev rules) to enlarge the root file system.
- use e2fsck -f /dev/mmcblk0p2 to perform a file system check.
Installed all lib<package>-dev and other libraries using apt-get. Then shutdown the QEMU
mkdir -p /tmp/rasp
sudo mount /home/test/raspberrypi/2014-01-07-wheezy-raspbian.img /tmp/rasp/ -o offset=$((122880*512))
cd $HOME/raspberrypi
sudo cp -ar /tmp/rasp ./rootfs
sudo chown -R $USER:$USER rootfs/home/pi
cd $HOME/raspberrypi/rootfs
# also small hack around: error "Never use <bits/predefs.h> directly; include <stdc-predef.h> instead."
sudo cp /home/test/raspberrypi/rootfs/usr/include/features.h /home/test/raspberrypi/rootfs/usr/include/features.h-
sudo cp /home/test/local/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot/usr/include/features.h /home/test/raspberrypi/rootfs/usr/include
sudo umount /tmp/rasp
I have also set up the target file system /home/test/raspberrypi/rootfs (This contains all the libraries necessary for cross compilation already installed)
The xtoolchain.cmake looks as follows
# ========================= CROSS COMPILATION INITIALIZATION BEGIN =======================
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
# specify the cross compiler
SET(CMAKE_C_COMPILER
$ENV{HOME}/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER
$ENV{HOME}/local/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)
# where is the target environment
#SET(CMAKE_FIND_ROOT_PATH
#/home/test/local/x-tools/arm-unknown-linux-gnueabi /home/test/raspberrypi/rootfs)
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspberrypi/rootfs)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#include_directories ("/home/test/local/raspidev/include")
# ========================= CROSS COMPILATION INITIALIZATION END =========================
The CMakeLists.txt (This is in addition to CMakeLists.txt in the parent directory as you can see from the file structure) portion inside the src directory looks like
cmake_minimum_required(VERSION 2.8)
...
...
...
#Change 1 to 0 to disable logging
add_definitions(-DENABLE_LOG=1)
set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(CMAKE_LIBRARY_PATH $ENV{HOME}/raspberrypi/rootfs/lib:$ENV{HOME}/raspberrypi/rootfs/usr/lib:$ENV{HOME}/raspberrypi/rootfs/usr/local/lib)
set(CMAKE_MODULE_PATH $ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules)
#rdynamic is for stack unwinding on crash
set(CMAKE_CXX_FLAGS "-W -O3 -g -Wall -std=c++0x -rdynamic -D_AIBTRACE ${CMAKE_CXX_FLAGS}")
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS})
include_directories ("${PROJECT_SOURCE_DIR}/headers")
#add_subdirectory (headers)
...
...
...
# ======================== SQLITE3 =================================
pkg_check_modules(SQLITE3 REQUIRED sqlite3)
include_directories(${SQLITE3_INCLUDE_DIRS})
...
... (...And other libraries)
I do an out of source build inside the x_build directory. as follows
cmake -DCMAKE_TOOLCHAIN_FILE=/home/test/xcompile/cpp/xtoolchain.cmake ../
Strangely, it seems to be linking against the host libraries as opposed to the target libraries. make VERBOSE=1 shows this
It throws linker error for all the libraries (These libraries are definitely present on the target system rootfs)
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lgmodule-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lgthread-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lxml2
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lglib-2.0
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -lsqlite3
/home/test/local/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.7.3/../../../../arm-unknown-linux-gnueabi/bin/ld: cannot find -ldbus-1
collect2: error: ld returned 1 exit status
I am a bit lost.
Edit:
As requested here are few more things from CMakeLists.txt
set (SOURCES
file1.cc
file2.cc
main.cc
)
# Similarly, why not concise files grouping that will be reused ;)?
set (COMMONS_LIBS
${OpenCV_LIBS}
pthread
${GLIBMM_LIBRARIES}
${GTKMM_LIBRARIES}
#${GSTMM_LIBRARIES}
${GSTREAMER_LIBRARIES}
${SQLITE3_LIBRARIES}
#${DBUS_GLIB_LIBRARY}
${DBUS_LIBRARIES}
${DBUS_GLIB_LIBRARIES}
${OPENSSL_LIBRARIES}
)
add_executable (
${EXE_DAEMON}
${SOURCES}
)
target_link_libraries(
${EXE_DAEMON}
$COMMONS_LIBS}
)
install (TARGETS
${EXE_DAEMON}
DESTINATION bin)
I added the following to CMakeLists.txt and then few more changes below and got it working!
include($ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules/FindPkgConfig.cmake)
set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(SYSROOT $ENV{HOME}/raspberrypi/rootfs/)
set (PKG_CONFIG_SYSROOT_DIR ${SYSROOT})
set(PKG_CONFIG_PATH ${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/local/lib/pkgconfig:${SYSROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${SYSROOT}/usr/share/pkgconfig)
#Change 1 to 0 to disable logging
add_definitions(-DENABLE_LOG=1)
find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config PATHS /usr/bin DOC "pkg-config executable" NO_CMAKE_FIND_ROOT_PATH)
set (CMAKE_CXX_FLAGS "-W -O3 -g -Wall -std=c++0x -rdynamic -D_TRACE ${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE)
#rdynamic is for stack unwinding on crash
set(CMAKE_PREFIX_PATH $ENV{HOME}/raspberrypi/rootfs/)
set(CMAKE_LIBRARY_PATH $ENV{HOME}/raspberrypi/rootfs/lib:$ENV{HOME}/raspberrypi/rootfs/usr/lib:$ENV{HOME}/raspberrypi/rootfs/usr/local/lib)
set(CMAKE_MODULE_PATH $ENV{HOME}/raspberrypi/rootfs/usr/share/cmake-2.8/Modules)
include_directories(SYSTEM
$ENV{HOME}/raspberrypi/rootfs/usr/include/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/usr/include
$ENV{HOME}/raspberrypi/rootfs/usr/local/include
)
add_definitions(
-march=armv6zk
-mfpu=vfp
-mfloat-abi=hard
)
link_directories(
$ENV{HOME}/raspberrypi/rootfs/lib/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/lib
$ENV{HOME}/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
$ENV{HOME}/raspberrypi/rootfs/usr/lib
$ENV{HOME}/raspberrypi/rootfs/usr/local/lib
)
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS})
Then did the following
sudo vim /home/test/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libpthread.so
edited
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/arm-linux-gnueabihf/libpthread.so.0 /usr/lib/arm-linux-gnueabihf/libpthread_nonshared.a )
to
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libpthread.so.0 libpthread_nonshared.a )
similarly,
sudo vim /home/test/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libc.so
edited
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( /lib/arm-linux-gnueabihf/libc.so.6 /usr/lib/arm-linux-gnueabihf/libc_nonshared.a AS_NEEDED ( /lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 ) )
to
OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libc.so.6 libc_nonshared.a AS_NEEDED ( ld-linux-armhf.so.3 ) )
Lastly, I added the -lpcre library to my COMMON_LIBS
Compile was successful. I then booted the raspberrypi via QEMU and used scp scp daemon pi#<ip address>:~ to transfer the resulting binary to PI emulator and ran it. Tadaa!!! it works fine!
I am developing a small simulation software that depends on two libraries, the GSL and the libconfig. As a build system, I use CMake. For both the GSL and libconfig, I found cmake files and copied them into the cmake/ directory of my project.
The scenario is the following: I want the project to have several different build types, like debug, release, etc., but also a custom one called cluster, which adds -static to the GCC flags and links against the .a libraries of the GSL and the libconfig, which I assume exist.
My CMakeLists.txt looks like this so far:
# version
SET(PACKAGE_VERSION "1.0")
SET(PACKAGE_NAME "INTERFACE")
PROJECT(interface C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# dirs -----------------------------------------------------
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
FIND_PACKAGE(GSL REQUIRED)
INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIRS})
SET(LIBS ${LIBS} ${GSL_LIBRARIES})
FIND_PACKAGE(LibConfig REQUIRED)
INCLUDE_DIRECTORIES(${LIBCONFIG_INCLUDE_DIRS})
SET(LIBS ${LIBS} ${LIBCONFIG_LIBRARIES})
CONFIGURE_FILE("res/config.h.in" "config.h")
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
SET_DIRECTORY_PROPERTIES(PROPERTIES
ADDITIONAL_MAKE_CLEAN_FILES "config.h"
)
# compilation ----------------------------------------------
ADD_EXECUTABLE( interface
interface.c interface.h config.h
helpers.c
output.c
lattice.c
genetic.c
)
# optional CFLAGS
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -std=c99")
SET(CMAKE_C_FLAGS_RELEASE "-O3 -ffast-math")
SET(CMAKE_C_FLAGS_CLUSTER "-O3 -ffast-math -static")
SET(CMAKE_C_FLAGS_DEBUG "-g")
SET(CMAKE_C_FLAGS_PROFILE "-g -ffast-math -pg")
TARGET_LINK_LIBRARIES(interface m ${LIBS})
# installation --------------------------------------------
INSTALL(TARGETS interface DESTINATION bin)
This adds the -static to the compiler, when I use -DCMAKE_BUILD_TYPE=cluster. The thing is, that it still links against the .so versions of the libs, which causes gcc to throw errors. At least the FindLibConfig.cmake scripts sets both a LIBCONFIG_LIBRARY and a LIBCONFIG_STATIC_LIBRARY variable, which I could use.
What is the most elegant or smart way to reach my goal?
Acording to cmake documentation, cmake uses the variable BUILD_SHARED_LIBS to determine the default for add_library().
If you set to ON cmake will assume all add_library() call will be as
add_library(target SHARED lib1 lib2 ...)
For example something like -DBUILD_SHARED_LIBS=ON on the cmake command line may do what you are asking.
I solved it like this:
The User can specify an additional variable -DSTATIC_LINKING=TRUE. Then, the script looks like this. (Only the important parts for the static linking and compilation are shown!)
# determine, whether we want a static binary
SET(STATIC_LINKING FALSE CACHE BOOL "Build a static binary?")
# do we want static libraries?
# When STATIC_LINKING is TRUE, than cmake looks for libraries ending
# with .a. This is for linux only!
IF(STATIC_LINKING)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
ENDIF(STATIC_LINKING)
# set -static, when STATIC_LINKING is TRUE and set LINK_SEARCH_END_STATIC
# to remove the additional -bdynamic from the linker line.
IF(STATIC_LINKING)
SET(CMAKE_EXE_LINKER_FLAGS "-static")
SET_TARGET_PROPERTIES(surface PROPERTIES
LINK_SEARCH_END_STATIC 1)
ENDIF(STATIC_LINKING)