cmake link xlib directories c++ - c++

I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})

In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})

I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.

Related

Problem with building c++ project with opencv and dlib libraries via cmake

I use c++ project with opencv and dlib libraries. I have CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)
project(project1)
find_package(OpenCV REQUIRED)
include(FetchContent)
FetchContent_Declare(dlib
GIT_REPOSITORY https://github.com/davisking/dlib.git
GIT_TAG v19.18
)
FetchContent_MakeAvailable(dlib)
add_executable(project1 main.cpp)
target_link_libraries(project1 ${OpenCV_LIBS} dlib::dlib)
target_compile_options(project1 PUBLIC "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")
I write in cygwin cmake .. -G "Unix Makefiles". I use "Unix Makefiles" to create makefile that use to command "make". After this I write make, but as a result I get error:
collect2: error: ld execution ended with return code 1
make[2]: *** [CMakeFiles/project1.dir/build.make:123: project1.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/project1.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
What I need to do to fix this problem?

How to link libraries properly using CMakeLists.txt for MinGW?

I have been pulling out of my hair because of CMakelists.txt. I would like to create a C++ application with GLFW, GLEW libraries, using MinGW and CLion IDE.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.3)
project(imguiTask)
set(CMAKE_CXX_STANDARD 17)
add_compile_options(-DGLEW_NO_GLU)
add_executable(${PROJECT_NAME}
src/main.cpp
src/imgui.cpp
src/imgui_draw.cpp
src/imgui_widgets.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC includes)
add_library(libraries STATIC IMPORTED)
target_link_libraries(${PROJECT_NAME} glfw3)
I placed the needed libraries in the libraries folder as you can see in the picture. The folder is located in the project root. It is giving me the following error:
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [imguiTask.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/imguiTask.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/imguiTask.dir/rule] Error 2
mingw32-make.exe: *** [imguiTask] Error 2
It seeems to me, that it does not search in the folder I specified. I don't know why.

How to integrate QtMultimedia component into a project using CMake?

I'm trying to write a GUI appication (under Ubuntu 18.04) using Qt5 and CMake. I failed to integrate the Qt multimedia module into my project.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(Chess)
#set(CMAKE_PREFIX_PATH "/home/mashplant/Qt5.9.6/5.9.6/gcc_64")
#whether I comment it off or not doesn't have an effect on the result
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5 COMPONENTS Widgets Network Multimedia REQUIRED)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
add_executable(Main Main.cpp
MainWindow.cpp MainWindow.hpp ChessFrame.cpp ChessFrame.hpp resource.qrc)
target_link_libraries(Main Qt5::Widgets Qt5::Network Qt5::Multimedia)
And I get the following warning when I run CMake:
CMake Warning at CMakeLists.txt:18 (add_executable):
Cannot generate a safe runtime search path for target Main because files in
some directories may conflict with libraries in implicit directories:
runtime library [libQt5Widgets.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Network.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Gui.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
runtime library [libQt5Core.so.5] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib
Some of these libraries may not be found correctly.
And when I compile, I get a link error.
/home/mashplant/Qt5.9.6/5.9.6/gcc_64/lib/libQt5Multimedia.so.5.9.6:undefined reference to ‘operator delete(void*, unsigned long)#Qt_5’
collect2: error: ld returned 1 exit status
CMakeFiles/Main.dir/build.make:203: recipe for target 'Main' failed
make[3]: *** [Main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Main.dir/all' failed
make[2]: *** [CMakeFiles/Main.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/Main.dir/rule' failed
make[1]: *** [CMakeFiles/Main.dir/rule] Error 2
Makefile:118: recipe for target 'Main' failed
make: *** [Main] Error 2
My best bet is that CMake find different components from different installations of Qt. If that's the case, it should be enough to correct all Qt-related CMake variables to point to correct paths (for example, variables like Qt5Widgets_DIR, Qt5Multimedia_DIR, etc.).

C++ 64bit project with PDCurses static link library via Clion's CMake

Recently I have acquired the "curses.h" and built the PDCurses "pdcurses.a" library file thanks to:
https://github.com/Alexpux/MINGW-packages/tree/master/mingw-w64-pdcurses
package. I also have prepared cmake files:
# pdcurses-config.cmake
set(PDCURSES_LIBDIR "${PROJECT_SOURCE_DIR}/lib")
set(PDCURSES_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/src/include")
set(PDCURSES_LIBRARIES "-L${PDCURSES_LIBDIR} -lpdcurses -static -Wall -Werror")
string(STRIP "${PDCURSES_LIBRARIES}" PDCURSES_LIBRARIES)
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MatrixAlgebra)
set(CMAKE_CXX_STANDARD 11)
set(PDCURSES_DIR "${PROJECT_SOURCE_DIR}/cmake")
find_package(PDCURSES REQUIRED)
include_directories(${PDCURSES_INCLUDE_DIRS})
set(SOURCE_FILES src/main.cpp)
add_executable(MatrixAlgebra ${SOURCE_FILES})
target_link_libraries(MatrixAlgebra ${PDCURSES_LIBRARIES})
Unfortunately I'm unable to link a simple "Hello World!" console program because either I am getting this:
mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpdcurses
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: * [CMakeFiles\MatrixAlgebra.dir\build.make:97: MatrixAlgebra.exe] Error 1
mingw32-make.exe[2]: [CMakeFiles\Makefile2:67: CMakeFiles/MatrixAlgebra.dir/all] Error 2
mingw32-make.exe[1]: [CMakeFiles\Makefile2:79: CMakeFiles/MatrixAlgebra.dir/rule] Error 2
mingw32-make.exe: * [Makefile:117: MatrixAlgebra] Error 2
or this (when I change "pdcurses.a" to "libpdcurses.a"):
Process finished with exit code -1073741515 (0xC0000135)
I simply don't know what to do to make it proceed without problems.
you shouldn't treat target_link_libraries() like commandline to feed it with parameters like -Wall
I don't know pdcurses, but when find_package finds that lib you should probably use something like:
target_link_libraries(MatrixAlgebra pdcurses::pdcurses)

CMake and Window .Lib Files

I have been struggling to find an answer to this question across Stackoverflow, at least one that I understand. I recently bought CLion and want to port an older project from Visual Studio over to it. But I have no idea how to link or add .lib files to my project. I need to link Xinput and DSound.
In Visual Studio I just added the headers and then added these to the top of my file
#pragma comment(lib, "XInput.lib")
#pragma comment(lib, "Dsound.lib")
I just have absolutely no idea how to link to those libs using CMake as I am a complete beginner with it.
Any Help would be gladly appreciated
This is my current CMake file
cmake_minimum_required(VERSION 3.3)
project("Handmade_Hero")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable("Handmade_Hero" ${SOURCE_FILES})
UPDATE
cmake_minimum_required(VERSION 3.3)
project(Handmade_Hero)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable(Handmade_Hero ${SOURCE_FILES})
set( LIBS XInput DSound )
target_link_libraries(Handmade_Hero ${LIBS} )
My Compiler now throws the following errors:
PATH\ClionProjects\Handmade-Hero\main.cpp:3:20: fatal error: Xinput.h: No such file or directory
#include <Xinput.h>
^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Handmade_Hero.dir/main.cpp.obj] Error 1
CMakeFiles\Handmade_Hero.dir\build.make:62: recipe for target 'CMakeFiles/Handmade_Hero.dir/main.cpp.obj' failed
mingw32-make.exe[2]: *** [CMakeFiles/Handmade_Hero.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Handmade_Hero.dir/rule] Error 2
mingw32-make.exe: *** [Handmade_Hero] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Handmade_Hero.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Handmade_Hero.dir/rule' failed
Makefile:117: recipe for target 'Handmade_Hero' failed
Simply add these after your CMakeLists.txt
SET( LIBS XInput DSound )
TARGET_LINK_LIBRARIES(projectname ${LIBS} )