linking librarys as a part of flags in toolchain file in cmake - c++

i've tried to add PROFILE to the CMAKE_BUILD_TYPES. For this I need to add -finstrument-functionsto the CMAKE_CXX_FLAGS in the toolchain file and link the belonging library with the absolut path in the CMakeLists.txt via target_link_libraries. So far so good and unproblematic.
But when refreshing the buildinformations cmake checks if the compiler stil does it's job. And now "is not able to compile a simple test program", due to the lack of the needed library, which is linked later in the CMakeLists.
So I commented the -finstrument-functions flag and the testprogramm could be compiled.
I've tried to:
- Set librarie and path as a part of the CXX flags with -L -l:
Use the link_directories(<dir>) command as well as SET(CMAKE_LINK_DIRECTORIES_BEFORE <Path> ) and SET(CMAKE_LIBRARY_PATH <Path>)
Set variables in the toolchainfile and call them later in the cmakelists:
SET(ADDITIONAL_PROFILE_LIBRARY "$ENV{QNX_BASE}/target/qnx7/armle-v7/usr/liblibprofilingS.a")
SET(ADDITIONAL_PROFILE_FLAGS "-finstrument-functions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" "${ADDITIONAL_PROFILE_FLAGS}")
add_executable(toolchainTester main.cpp)
target_link_libraries(toolchainTester ${ADDITIONAL_PROFILE_LIBRARY})
But then getting the error: mingw32-make.exe[3]: *** No rule to make target 'C:/qnx700/target/qnx7/armle-v7/usr/liblibprofilingS.a', needed by '../out/profileout'. Stop.
Got anyone any more ideas? Thanks in advance.

If there anyone out there, looking for the same workaround how to manage different flags and libraries for different profiles, this is one working way:
First set variables in the toolchainfile
SET(ADDITIONAL_PROFILE_LIBRARY "$ENV{QNX_BASE}/target/qnx7/armle-v7/usr/lib/libprofilingS.a")
SET(ADDITIONAL_CXX_FLAGS "-finstrument-functions")
The call/overright them in the CMakeLists.txt
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" "${ADDITIONAL_CXX_FLAGS}")
add_executable(toolchainTester main.cpp)
target_link_libraries(toolchainTester ${ADDITIONAL_PROFILE_LIBRARY})

Related

How do I link to a static library (libtiff) using CMake in my wxWidgets project?

For my wxWidgets project, I am trying to make the switch from my self-written Makefile to Cmake. I develop on macOS.
When I was writing that Makefile I ran into an issue with libtiff. I wanted to statically link my application so that I don't have to distribute any dylibs myself or rely on my users to install them. I built wxWidgets as a static library but when I compiled my code and checked my binary with otool I always found that my binary required a local dylib.
/usr/local/opt/libtiff/lib/libtiff.5.dylib
Finally I found a solution on here. In essence, in the linking line of my Makefile I replaced wx-config –-libs with LDFLAGS. LDFLAGS looks like this:
WXCONFIGLIBS := $(shell wx-config --libs)
WXCONFIGLIBS := $(WXCONFIGLIBS:-ltiff=/usr/local/opt/libtiff//lib/libtiff.a)
# I am not sure whether the double slash is a typo but it works so I don't change it
LDFLAGS := $(WXCONFIGLIBS)
Basically, I search-and-replaced -ltiff with the path to my static libtiff library.
Now I've managed to compile my project using Cmake. However, I'm getting the same warning message as I did when I battled my original issue.
ld: warning: dylib (/usr/local/lib/libtiff.dylib) was built for newer macOS version (11.0) than being linked (10.11)
How do I fix this? My CMakeLists contains these sections pertaining to wxWidgets:
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
...
add_executable(myapp ${SOURCES})
target_link_libraries(myapp ${wxWidgets_LIBRARIES})
set_property(TARGET myapp PROPERTY CXX_STANDARD 17)
I already tried running some search-and-replace shenanigans like
string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES ${wxWidgets_LIBRARIES})
But that doesn't work. It does replace -ltiff but also seems to remove the semicolons and whitespaces separating the different libraries.
I've been scouring the web for any clues as to what to do, but I don't seem to have a good enough grasp of libraries to fix this.
Any help would be greatly appreciated.
Set wxWidgets_USE_STATIC=ON before calling find_package(wxWidgets). See the documentation for wxWidgets here: https://cmake.org/cmake/help/latest/module/FindwxWidgets.html
option(wxWidgets_USE_STATIC "Link to wxWidgets statically" ON)
find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
...
add_executable(myapp ${SOURCES})
target_link_libraries(myapp PRIVATE ${wxWidgets_LIBRARIES})
target_compile_features(myapp PRIVATE cxx_std_17)
My search-and-replace idea turned out to be not so bad. I was able to achieve the same outcome with Cmake as with my Makefile.
My problem was not using double quotes in the appropriate place. So instead of this:
string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES ${wxWidgets_LIBRARIES})
I simply needed to write:
string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}")
So to solve my actual problem, I am calling this string() command just before the target_link_libraries() command.

Compiling Code with CMAKE written with C++ and DDS

I am new to using CMAKE and I am writing a Cmakelists.txt file to compile my project. I get the following error:
/usr/bin/ld: /home/tarun/rti_connext_dds-6.0.0/lib/x64Linux3gcc5.4.0/libnddsc.so: undefined reference to symbol 'PRESTypePlugin_interpretedSerialize'
/home/tarun/rti_connext_dds-6.0.0/lib/x64Linux3gcc5.4.0/libnddscore.so: error adding symbols: DSO missing from command line
I have tried other solutions already available like adding LFLAGS and CFLAGS but I still get the same error.
This is how I am linking my libraries in cmakelists
target_link_libraries(test_IMU_DDS PUBLIC
${CONNEXTDDS_CPP2_API_LIBRARIES_RELEASE_SHARED}
${CONNEXTDDS_EXTERNAL_LIBS}
${COMMANDTYPE_OBJS}
${PROJECT_SOURCE_DIR}/xspublic/xscommon
${PROJECT_SOURCE_DIR}/xspublic/xscontroller
${PROJECT_SOURCE_DIR}/xspublic/xstypes
${CMAKE_DL_LIBS}
)
Will anyone be able to help me out with this?
There is an example about how to build a ConnextDDS application in their community GitHub profile: Build a ConnextDDS application with CMake
That example is using the files from the "resources/cmake" folder (to call RTI Code Generator and a FindPackage CMake script). The FindPackage script is also provided with the ConnextDDS installation (6.0.0) but the one from the community is more updated. The FindPackage script will provide you with all the definitions, flags and libraries you need in order to integrate ConnextDDS in your project.
I added this nddscd, nddscored, nddscpp2 to target_link_libraries and these flags to CMakeLists.txt:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRTI_UNIX")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRTI_LINUX")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRTI_64BIT")

CMake and Make need to be run twice in order to build code successfully

I am using CMake 3.8.2, GNU make 4.2.1 and GCC 6.4.0 for my C++14 project and I noticed a strange behavior when building. I am using CMake for an out-of-source build in a sub-folder called "build" where I run cmake .. followed by make.
CMake runs fine without any errors and make will build all source files like I expect until it is done compiling and starts linking them. It will then fail with an error
[ 83%] ...
[100%] Linking CXX executable myproject
/usr/bin/ld: some-source-file.cc.o: undefined reference to symbol '_ZNKSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEE3strEv##GLIBCXX_3.4.21'
Interestingly it doesn't show any compiler warnings up to this point and only shows the above mentioned linker error.
Now when I ignore the error and simply run cmake .. and then make again (just like I did before) I get all the compiler warnings that my code should produce and everything links perfectly fine, even though I didn't change any code or CMake-related files in the meantime.
I can reproduce this behavior by deleting all files in the build dir by running rm -r *.
Here is my CMakeLists.txt file:
# Define minimum required CMake version
cmake_minimum_required(VERSION 3.8.2)
# Setting compiler related settings
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -O2 -lstdc++")
set(CMAKE_CXX_STANDARD 14)
# Define project name
project(MyProject)
# Find source files
file(GLOB_RECURSE SOURCES application/*.cc)
# Adding third-party sources
set(SOURCES ${SOURCES} "third-party/cpp-base64/base64.cpp")
# Executable to be built from which source files
add_executable(myproject ${SOURCES})
# Find and include and link Botan
find_library(BOTAN botan-2 "third-party/botan/build/lib")
include_directories("third-party/botan/build/include/botan-2")
# Includes that are part of the project
include_directories("${CMAKE_SOURCE_DIR}/application/include")
# Include nlohmann/json
include_directories("third-party/json/src")
# Include cpp-base64 by René Nyffenegger
include_directories("third-party/cpp-base64")
find_package(Boost REQUIRED COMPONENTS program_options)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
# Link third-party libraries
target_link_libraries(myproject ${Boost_LIBRARIES} ${BOTAN})
Note: I am required to check-in the compiler and libraries I am using, which is why I specified them in the CMake file.
If it only works the second time it has to do with cached variables.
So I'm pretty sure that it will work the first time if you modify CMAKE_CXX_COMPILER setting by adding set(... CACHE INTERNAL "") to:
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc" CACHE INTERNAL "")
And move set(CMAKE_CXX_FLAGS ...) after the project() command.
But please also be noted that you shouldn't put the compiler into your CMakeLists.txt.
References
CMake: In which Order are Files parsed (Cache, Toolchain, …)?
Passing compiler options cmake
CMake Error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found

Clion mongodb dependency setup

I am getting the following error:
--Configuring incomplete, errors occurred!
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
See also "C:/Users/GyuriX/.CLion2016.1/system/cmake/generated/Projects-33418280/33418280/Debug/CMakeFiles/CMakeOutput.log".
Please set them or make sure they are set and tested correctly in the CMake files:
BSON_LIBRARY
linked by target "Projects" in directory D:/DEV/C/Projects
MONGODB_LIBRARY
linked by target "Projects" in directory D:/DEV/C/Projects
Makefile:443: recipe for target 'cmake_check_build_system' failed
mingw32-make.exe: *** [cmake_check_build_system] Error 1
When I try to compile just a simple Hello World program using the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(Projects)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
include_directories(C:/mongo-c-driver/lib)
find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)
add_executable(Projects ${SOURCE_FILES})
target_link_libraries(Projects ${MONGODB_LIBRARY} ${BSON_LIBRARY})
I have compiled the required libraries based on what the mongodb wiki said, so I have the library files:
The files I have compiled
So the question is that what am I doing wrong and what should I do to be able to use the mongodb in CLion.
Error message means that find_library fails to find requested libraries.
As you install libraries into non-default directories (according to the screenshot, it is C:\mongo-c-driver\bin), you need to hint for find_library() where it should search.
One possible way is to set CMake variable CMAKE_LIBRARY_PATH:
set(CMAKE_LIBRARY_PATH "C:\\mongo-c-driver\\bin")
# Now find_library should be able to find libraries
find_library(MONGODB_LIBRARY mongoc-1.0)
find_library(BSON_LIBRARY bson-1.0)
There are others way for hint to find_library, see its documentation about search algorithm it uses.

how to include NTL using CMake

I use this line to compile a simple program:
g++ main.cc -lntl -lm -lgmp
How do you include this into CMake?
find_package(NTL REQUIRED)
find_package(GMP REQUIRED)
Doesn't work. And gives the following error:
CMake Error at CMakeLists.txt:30 (find_package):
Could not find module FindNTL.cmake or a configuration file for package
NTL.
...
.
and
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)
Doesn't work either (but I think it's just wrong in general).
Thank you!
If ntl, m, and gmp libraries are usually installed to somewhere in the default path (e.g. /usr/ or /usr/local/), you could simply do something like:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(test main.cc)
target_link_libraries(test ntl m gmp)
This is probably closest to your original g++ command, but it isn't very robust however; if any of the libraries aren't found, you won't know about it until you try linking. If you want to fail at configure time (i.e. while running CMake), you could add find_library calls for each of the required libs, e.g.
find_library(NTL_LIB ntl)
if(NOT NTL_LIB)
message(FATAL_ERROR "ntl library not found. Rerun cmake with -DCMAKE_PREFIX_PATH=\"<path to lib1>;<path to lib2>\"")
endif()
You'd then have to change your target_link_libraries command to
target_link_libraries(test ${NTL_LIB} ${M_LIB} ${GMP_LIB})
You'd probably also then have to do a find_file for one of each lib's header files to find out the appropriate path to add via the include_directories command (which translates to -I for g++).
Note, it's important to put quotes around the extra CXX_FLAGS arguments, or CMake treats them like separate values in a list and inserts a semi-colon between the flags.
For further information about find_library, find_file, etc. run:
cmake --help-command find_library
cmake --help-command find_file
Regarding your error:
It doesn't look like there's a FindNTL.cmake module included with CMake. That means you'll have to either:
Write your own FindNTL.cmake,
Find another that somebody else has written,
Hack together a solution that:
Checks if NTL is installed
Provides link targets, relevant flags, etc.
From a (rather quick) Google search, it appears somebody has an NTL module for CMake. Since NTL use GMP, you will probably need the associated GMP module for CMake. It doesn't look like a fully-featured CMake module, but it also appears to be the best thing out there (again, it was a quick Google search, and I don't use NTL).
To use, you'll want to add some things to your CMakeLists.txt:
# Let CMake know where you've put the FindNTL.cmake module.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/CMake/Modules")
# Call the FindNTL module:
find_package(NTL REQUIRED)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++0x -lntl -lm -lgmp)
Yes, this is wrong. You don't want to be setting your CXX_FLAGS with linking directives. I would use:
SET ( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=cxx0x )
to set the Cxx standard you want to use. To actually link to libraries, you'll want to:
Ensure that you've found the libraries (with the relevant find_package ( FOO ) lines)
Link those against your target, like this:
# Build the Foo executable. (Or library, or whatever)
add_executable (FooEXE ${Foo_SOURCES} )
target_link_libraries (FooEXE
${bar_LIBRARIES}
${baz_LIBRARY}
)
Please note! ${bar_LIBRARIES} and ${baz_LIBRARY} is not a typo; there's no standard way of setting the relevant libraries in the FindFOO.cmake modules, which is, in my opinion, an annoyance. If one doesn't work, try the other, or, worst case, have a look in the relevant FindFOO.cmake file (there's a bunch installed with CMake by default) to see what each one uses. With the link i provided, you can use ${NTL_LIB} and ${GMP_LIB}.