Running Cmake built executables with shared library dependencies - cmake

I have a Cmake executable that has been created using ADD_EXECUTABLE.
The executable needs (links to) shared libraries that have been created
also part of the project.
As a final step I want to run this executable as part of the build process to
create some additional files for the build.
so
ADD_EXECUTABLE(foo)
ADD_CUSTOM_COMMAND(OUTPUT out1
WORKING_DIRECTORY dir1
COMMAND foo args
...)
However when the custom command runs it gives an error
.../foo: error while loading shared libraries: libbar.so.1: cannot open shared object file: No such file or directory
make[2]: *** [out1] Error 127
make: *** [all] Error 2
The problem is that the LD_LIBRARY_PATH is not set for the executable to load the library
libbar.so.1. How do in a cross-platform manner set the load library settings in cmake.
I don't want to use RPATH because it actually embeds those paths in the executable, which I
don't want.

Related

.lst file generation in cmake

I want to generate .lst files for my source file. I am successfully able to generate the executable.
But I don't know how to generate .lst files for my source files.
get_property(allsrcfiles TARGET myExe PROPERTY SOURCES)
foreach(srcfile IN LISTS allsrcfiles)
set_source_files_properties(${srcfile} PROPERTIES COMPILE_FLAGS "-Wa, anhlmsd=${srcfile}.lst")
endforeach()
I have added the above script in my CMakelists.txt, if I add the above script I am getting the following error
[100%] Linking C executable myExe
dld: error: Can't open file 'CMakeFiles/myExe.dir/src/sample.c.obj': No such file or directory
CMakeFiles\myExe.dir\build.make:101: recipe for target 'myExe' failed gmake[2]: *** [myExe] Error 1
CMakeFiles\Makefile2:93: recipe for target 'CMakeFiles/myExe.dir/all' failed
gmake[1]: *** [CMakeFiles/myExe.dir/all] Error 2
Makefile:101: recipe for target 'all' failed
gmake: *** [all] Error 2
sample.c.obj file is getting generated without source_file_properties.
compiler : WIND RIVER
Target : ppc - embedded
Host : windows
is list file generation commands depends on the compiler?

How can I include a dependency file generated by a custom command in cmake for GNU make?

I have integrated a tool that generates some code from a text file in CMake based build environment.
This tool is also able to generate the dependency file in the GNU make format (pretty much like gcc -MD does it for c files).
I would like to include this dependency file in makefiles generated by CMake in order to correctly rebuild when necessary. Unfortunately, I could not find the correct way to do this.
I tried:
Generated Dependency Files in CMake , but there was no answer
https://cmake.org/cmake/help/latest/command/add_custom_command.html?highlight=add_custom_command has the DEPFILE option but it only works for Ninja
Has anyone had a similar issue?
try to define your file as dependency of your add_custom_target:
cmake_minimum_required(VERSION 3.0)
project(Custom_Command_TEST)
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/your_gen_file.txt"
COMMAND /bin/date > "${CMAKE_BINARY_DIR}/your_gen_file.txt"
COMMAND /bin/echo "RUNNING COMMAND")
add_custom_target(GenerateFile
/bin/echo "RUNNING TARGET"
DEPENDS "${CMAKE_BINARY_DIR}/your_gen_file.txt")
add_executable(${PROJECT_NAME} main.cpp)
add_dependencies(${PROJECT_NAME} GenerateFile)
will only rebuild the target GenerateFile. You could put your .txt generation step as COMMAND in add_custom_command.
output of first make:
[ 33%] Generating your_gen_file.txt
RUNNING COMMAND
RUNNING TARGET
[ 33%] Built target GenerateFile
[ 66%] Building CXX object CMakeFiles/Custom_Command_TEST.dir/main.cpp.o
[100%] Linking CXX executable Custom_Command_TEST
[100%] Built target Custom_Command_TEST
output second make:
RUNNING TARGET
[ 33%] Built target GenerateFile
[100%] Built target Custom_Command_TEST
In case you want to rebuild everything again, you'll want to make clean.
You could also check this older post

You have called ADD_LIBRARY for library pugixml without any source files

I am working on quickrank: https://github.com/hpclab/quickrank. when I compile it I get error
cmake .. -DCMAKE_CXX_COMPLIER=/usr/bin/g++ -DCMAKE_BUILD_TYPE=Release
You have called ADD_LIBRARY for library pugixml without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Configuring done
CMake Error: Cannot determine link language for target "pugixml".
CMake Error: CMake can not determine linker language for target: pugixml
CMake Error: CMake can not determine linker language for target: pugixml
-- Generating done
-- Build files have been written to: /home/students/s4438236/quickrank/build_
s4438236#moss:~/quickrank/build_$ make
make[2]: *** No rule to make target `CMakeFiles/pugixml.dir/build'. Stop.
make[1]: *** [CMakeFiles/pugixml.dir/all] Error 2
make: *** [all] Error 2
I do find source file under the lib\pugixml folder, how can I fix this error?
When you call the add_library CMake command, you must provide source files for this target. If we examine the top-level CMakeLists.txt file, we see where the error is occurring:
# external libraries
file(GLOB_RECURSE pugixml_sources ${CMAKE_SOURCE_DIR}/lib/pugixml/src/*.cpp)
add_library(pugixml STATIC ${pugixml_sources})
The CMake error suggests that the pugixml_sources variable is empty, which hints that the /lib/pubixml may have also been empty. If you initially didn't run the git clone command with --recursive, you would not have gotten the pugixml submodule.
Seeing as you said the pugixml sources are now there, I would suggest deleting your CMake cache and the CMake build folder. Running CMake again from scratch will likely allow it to see the pugixml source files.

Target file fails during INSTALL stage with CMake

I have a CMakeLists.txt file that is building a shared lib as en ExternalProject and this lib needs to be installed in a specific location so that it can be picked up by the main project at build time. I am using the following line to perform the install process:
install(FILES $<TARGET_FILE:of_shared> DESTINATION oF/lib)
However, this fails:
Install the project...
-- Install configuration: ""
CMake Error at cmake_install.cmake:31 (FILE):
file INSTALL cannot find
"/Users/me/packages/builds/x86_64/of-0.9.3-osx-release/$<TARGET_FILE:of_shared>".
make[3]: *** [install] Error 1
make[2]: *** [of_shared-prefix/src/of_shared-stamp/of_shared-install] Error 2
make[1]: *** [CMakeFiles/of_shared.dir/all] Error 2
make: *** [all] Error 2
This might be a compatibility bug of some sort, maybe between CMake versions. I am using CMake 2.8.12.
These are the related CMake lines:
add_library(core OBJECT ${OF_SOURCE_FILES})
add_library(of_shared SHARED $<TARGET_OBJECTS:core>)
set_target_properties(of_shared PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set_target_properties(of_shared PROPERTIES OUTPUT_NAME openFrameworks)
target_link_libraries(of_shared ${OF_CORE_FRAMEWORKS} ${OF_ADDON_FRAMEWORKS} ${OF_CORE_LIBS} ${OF_ADDON_LIBS})
install(FILES $<TARGET_FILE:of_shared> DESTINATION oF/lib)
install(FILES libs/fmodex/lib/osx/libfmodex.dylib DESTINATION oF/lib)
install(DIRECTORY libs/glut/lib/osx/GLUT.framework DESTINATION .)
Obviously that bit with the $<TARGET_FILE:of_shared> is failing. Apart from that, everything is working. I can see the shared lib being created.
I have found the solution myself. Instead of using:
install(FILES $<TARGET_FILE:of_shared> DESTINATION openFrameworks/lib)
One should use:
install(TARGETS of_shared LIBRARY DESTINATION openFrameworks/lib)
In that case of_shared is the name of the library.

CMake: satisfying dependencies when building shared object file (.so) from object file (.o)

I am able to do this without CMake using a handwritten Makefile, like so:
g++ $(CXSCINC) -c -fPIC cellComplex_extern.cpp -o cellComplex_extern.o
g++ $(CXSCINC) -shared -Wl -o cellComplex_lib.so cellComplex_extern.o $(CXSCLIB) -lcxsc
This gets me shared library cellComplex_lib.so, which then gets picked up by ctypes as a dynamically linked library (lib = ctypes.cdll.LoadLibrary('./cellComplex_lib.so') for later use.
My project has moved to CMake as a build system and I am looking to emulate the functionality of my Makefile above.
So far I have discovered the add_library() command for CMakeLists.txt, but the link to the CXSC library is never made (as evidenced by horrible complaining when I run make after cmake.
How can I tell CMake to build cellComplex_lib with the third-party library CXSC?
-- non-working CMakeLists.txt --
add_library(include/python/cellComplex_extern OBJECT
include/python/cellComplex_extern.cpp ${all_headers})
add_library(include/python/cellComplex_lib SHARED
include/python/cellComplex_extern)
target_link_libraries(include/python/cellComplex_lib ${CXSC_LIB_DIR}/libcxsc.a)
Result of running cmake followed by make:
.
.
.
[ 75%] Built target include/python/cellComplex_extern
Linking CXX shared library libinclude/python/cellComplex_lib.dylib
ld: can't open output file for writing: libinclude/python/cellComplex_lib.dylib, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libinclude/python/cellComplex_lib.dylib] Error 1
make[1]: *** [CMakeFiles/include/python/cellComplex_lib.dir/all] Error 2
make: *** [all] Error 2
I think you need to use target_link_libraries
target_link_libraries(include/python/cellComplex_lib ${CXSLIB})
This is what I use during Win32 development:
link_directories(${LIB_ROOT_DIR}/lib ${LIB_ROOT_DIR2}/lib/morelibs)
add_library(MyDll1 SHARED File1.cpp File2.cpp)
add_library(MyDll2 SHARED File3.cpp File4.cpp)
add_dependencies(MyDll2 Dll1)
target_link_libraries(MyDll2 Dll1 some.lib another.lib)
Here you specify that Dll2 requires Dll1 and two other external lib's.