CMAKE and MKOCTFILE - c++

I'm working on a C/C++ project and I'm using CMAKE 3.5.2 for build. However, now I must to include a C++ file which uses Octave functions. I am able to compile this source file directly by line command using this command: mkoctfile --link-stand-alone new_oct_file -o final_library.
I'm struggling to do CMAKE execute this command. I've tried to use an add_custom_command, but it didn't work. Can someome help me?
My CMAKE has the following structure
cmake_minimum_required(VERSION 2.8)
project(final_library)
add_executable(final_library program.c
./Commons/util.c
./Tools/xulambs_tool.cpp)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
include_directories(/usr/include/octave-4.0.0/octave)
install(TARGETS final-library RUNTIME DESTINATION bin)
add_subdirectory(Commons)
add_subdirectory(Tools)
I've tried to add the following command (it does not work):
set(MKOCTFILE "mkoctfile")
set(OCTARG "--link-stand-alone")
add_custom_command(TARGET reordering-library
PRE_LINK
COMMAND ${MKOCTFILE} ARGS ${OCTARG} ./Tools/tool_octave.cpp)
The compilation output is
[ 4%] Linking CXX executable final-library
g++: error: ./Tools/tool_octave.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
CMakeFiles/final-library.dir/build.make:694: recipe for target 'final-library' failed
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/final-library.dir/all' failed
Makefile:127: recipe for target 'all' failed
make[2]: *** [final-library] Error 1
make[1]: *** [CMakeFiles/final-library.dir/all] Error 2
make: *** [all] Error 2
Thanks.

I had the same problem as you and I fixed it by adding the liboctinterp.so in the target_link_libraries of the CMakeLists.txt file.
My current cmake file contains the following:
add_executable(MyEXE main.cc)
target_link_libraries(MyEXE liboctave.so liboctinterp.so)

The command at pre link is probably not executed in the source directory, so the relative path you used in the script will be invalid. Try using an absolute path, something like:
add_custom_command(TARGET reordering-library
PRE_LINK
COMMAND ${MKOCTFILE} ARGS ${OCTARG}
"${CMAKE_CURRENT_SOURCE_DIR}/Tools/tool_octave.cpp"
)

Related

Why does gcc can't find opencv.hpp file?

I'm quite new to CMake, but I want to build a test .cpp file that includes OpenCV and shows me an image. I have built OpenCV in the path /usr/local and I have here folder with opencv.hpp file - /usr/local/include/opencv4/opencv2/opencv.hpp. Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
project(cpp_proj)
find_package(OpenCV REQUIRED)
add_executable(cpp_proj opencv_cpp.cpp)
include_directories(${OPENCV4_INCLUDES})
target_link_libraries(cpp_proj )
I opened ~./bashrc and added there lines:
export OPENCV4_INCLUDES=/usr/local/include/
export OPENCV4_LIBRARIES=/usr/local/lib/
export PATH=$PATH:$OPENCV4_LIBRARIES
export PATH=$PATH:$OPENCV4_INCLUDES
When I run cmake in bash - everything is ok and even find_package finds OpenCV. But when I'm trying to run make it gives me a error:
pi#raspberrypi:~/Desktop/cpp_proj/build $ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Desktop/cpp_proj/build
pi#raspberrypi:~/Desktop/cpp_proj/build $ make
[ 50%] Building CXX object CMakeFiles/cpp_proj.dir/opencv_cpp.cpp.o
/home/pi/Desktop/cpp_proj/opencv_cpp.cpp:1:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include <opencv2/opencv.hpp>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/cpp_proj.dir/build.make:63: CMakeFiles/cpp_proj.dir/opencv_cpp.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/cpp_proj.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I found questions with the same problem, but they didn't help me. What I am doing wrong? What could be the reason of this? Thanks!
In order to use a library you must specify the include directory as well as the libs.
With find_package (in module mode), in your case it should populate the variables OpenCV_INCLUDE_DIRS and OpenCV_LIBS for you to use.
So I recommend to add / alter your code to add the include directory & link the library (such as below)
target_include_directories(${CMAKE_PROJECT_NAME} public ${OpenCV_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} public ${OpenCV_LIBS})
I don't believe you ever need to touch ~./bashrc when using find_package

CMake static library not found

im trying to link a static library (libglfw3.a). The library is place in my "lib" folder which is in the root directory of my project. I keep getting the error which can be found at the bottom. Hope someone can help me. Thanks in advance!
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(GLFWGLADSTUFF)
set(CMAKE_CXX_STANDARD 11)
set(SOURCES
src/main.cpp
src/glad.c
)
INCLUDE_DIRECTORIES(include)
add_executable(GLFWGLADSTUFF ${SOURCES})
target_include_directories(GLFWGLADSTUFF
PUBLIC src
PUBLIC include
)
target_link_libraries(GLFWGLADSTUFF
lib/libglfw3.a
)
Error:
Consolidate compiler generated dependencies of target GLFWGLADSTUFF
[ 33%] Linking CXX executable GLFWGLADSTUFF
ld: library not found for -llib/libglfw3.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GLFWGLADSTUFF] Error 1
make[1]: *** [CMakeFiles/GLFWGLADSTUFF.dir/all] Error 2
make: *** [all] Error 2
link_directories(lib)
target_link_libraries(GLFWGLADSTUFF glfw3)
should resolve your issue. CMake is not recognizing your path as a path, but as the library name.
target_link_libraries(GLFWGLADSTUFF
lib/libglfw3.a
)
Will not work. You can:
specify the linker path and the name to be linked (i.e. other answer)
or preferably, use an IMPORTED add_library. There is even nice cmake documentation on the topic.

How to link SDL2 manually in CMake

Recently I have started to learn CMake. To practice, I am trying to link SDL2 manually. I know that there is another way around using find_file which is easy. But I want to do it myself for practice.
I get an error when I try to link the libSDL2main.a file (running the Makefile using cmd mingw32-make)
[ 50%] Linking CXX executable exe0.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -llibSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\exe0.dir\build.make:105: recipe for target 'exe0.exe' failed
mingw32-make[2]: *** [exe0.exe] Error 1
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/exe0.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/exe0.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SDL_Test_Project)
include_directories(include)
add_executable(exe0 main.cpp)
target_link_libraries(exe0 libSDL2main.a)
Here main.cpp is only a source file. I have put SDL2.dll and libSDL2main.a into the root of the project directory. (I used the CMake GUI to generate the Makefile in Windows 10).
If you want to link to the SDL2 libraries directly in target_link_libraries() (without defining IMPORTED targets, or using find_library()), use the full path to each library. The CMAKE_SOURCE_DIR variable provides the full path to the root directory of the CMake project:
target_link_libraries(exe0 PRIVATE
mingw32
${CMAKE_SOURCE_DIR}/libSDL2main.a
${CMAKE_SOURCE_DIR}/SDL2.dll
)
Note, for SLD2, you may also have to add the mingw32 to this command when using MinGW for compilation.

Build a project depending on the Swiften XMPP library with CMake

I am currently trying to setup my C++ project using CMake, I successfully included dependencies like OpenMPI, but when going to Swiften, this became complex. This is not delivered in the form a package, but as shown in the git repository linked up there. Building that wasn't an issue, but I searched quite a lot of time, without finding any way to include libraries that don't provide a .so, and no main headers.
Here's my current CMakeLists.txt, made for testing only.
cmake_minimum_required (VERSION 3.0.0)
#set the executable
project (mtest)
set(EXECUTABLE_OUTPUT_PATH .)
add_executable (mtest main.cpp)
#defines libs
find_package(MPI REQUIRED)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
include_directories(SYSTEM Swiften)
target_link_libraries(mtest ${MPI_LIBRARIES})
add_library(swiften SHARED IMPORTED)
set_target_properties(swiften PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/Swiften/libSwiften.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/Swiften"
)
target_link_libraries(mtest swiften)
And when trying to run make, here's the error :
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Documents/CTests/build
[ 50%] Building CXX object CMakeFiles/mtest.dir/main.cpp.o
/home/user/Documents/CTests/main.cpp:12:10: fatal error: Swiften/Client/Client.h: No such file or directory
#include <Swiften/Client/Client.h>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/mtest.dir/build.make:62: recipe for target 'CMakeFiles/mtest.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/mtest.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mtest.dir/all' failed
make[1]: *** [CMakeFiles/mtest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
By the way, if the directory structure may help you, here's a tree output : https://pastebin.com/giPb9229

Error in Linking a .so library in macOS using CMake

I am trying to link a .so file that is named libtwitcurl.so.1 using CMake. My Cmake file looks like this:
cmake_minimum_required(VERSION 3.8)
project(MarkoTweeter)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp markov/markov_chain.cpp markov/markov_chain.h libraries libraries/curl)
include_directories(${CMAKE_SOURCE_DIR}/inc)
link_directories(${CMAKE_SOURCE_DIR}/libraries)
add_executable(MarkoTweeter ${SOURCE_FILES} markov/markov_chain.cpp
markov/markov_chain.h)
target_link_libraries(MarkoTweeter twitcurl)
But I keep getting this error:
[ 33%] Linking CXX executable MarkoTweeter
ld: library not found for -ltwitcurl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [MarkoTweeter] Error 1
make[2]: *** [CMakeFiles/MarkoTweeter.dir/all] Error 2
make[1]: *** [CMakeFiles/MarkoTweeter.dir/rule] Error 2
make: *** [MarkoTweeter] Error 2
For some reason it cannot find the shared library. I have tried using:
g++ main.cpp libraries/libtwitcurl.so.1
Which works fine. But I can't seem to make it work with CMake in CLion.
You need to pass absolute path to target_link_libraries.
Use find_library instead of link_directories as recommended in the official documentation:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
Simple usage of find_library for your case would be:
find_library(TWIT_CURL_LIBRARY twitcurl ${CMAKE_SOURCE_DIR}/libraries)
target_link_libraries(MarkoTweeter ${TWIT_CURL_LIBRARY})