I'm working on a simple tutorial from here: https://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php
My initial program did not do any image loading. It just showed a screen and went away. Everything worked fine. Here is my initial CMakeLists.txt file:
cmake_minimum_required(VERSION 3.7)
project(01_hello_world)
set(CXX_STANDARD 17)
find_package(SDL2 REQUIRED)
add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES})
Everything worked and compiled just fine. However, I then wanted to load a PNG image, which I thought would be a very easy. Googling led me to the SDL2 Image library, and the IMG_Load method. So I went ahead and installed libsdl2-image-dev, and my CMakeLists.txt file grew by two more lines::
set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
However, just by using the IMG_Load method, the make command threw a whole bunch of library requirements: libtiff-dev, libpng-dev, libjpeg-dev, libwebp-dev. All this just to load a png file! So I went ahead and installed all those, and now my CMakeLists.txt file looks like this abomination (I used find_package where I could, and manually set variables where I couldn't):
cmake_minimum_required(VERSION 3.7)
project(01_hello_world)
set(CXX_STANDARD 17)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)
find_package(TIFF REQUIRED)
set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
set(WEBP_LIBRARIES "/usr/lib/x86_64-linux-gnu/libwebp.a")
add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARY} ${WEBP_LIBRARIES} ${TIFF_LIBRARIES} Threads::Threads)
At this point, when I make, I get this error:
[ 50%] Linking CXX executable hello
/usr/bin/ld: cannot find -l{TIFF_LIBRARIES}
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:98: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I've been googling for hours. Can someone provide a light in this darkness?
Also is this the correct approach to working with sdl + cmake?
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
It seems like you aren't dynamically linking SDL2_image to your program.
I don't know how to change it in CMake,but you could compile your program utilizing this pattern:
g++ your_program.cpp -o your_program -lSDL2 -lSDL2_image
You may also utilize a Makefile like this one:
OBJS = your_program.cpp
OBJ_NAME = your_program_output
all:
g++ $(OBJS) -o $(OBJ_NAME) -lSDL2 -lSDL2_image # don't forget about replacing the four spaces by a tab character
One last thing, lazy foo has a tutorial teaching how to install and set up the SDL2_image library on linux utilizing an IDE (Code::Blocks) or not (command lines and Makefiles).
http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/linux/index.php
Related
I'm trying to compile and execute a simple test program with curl lib.
#include <curl/curl.h>
using namespace std;
int main(){
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_global_cleanup();
return 0;
}
I have a CMakeLists to generate MakeFile :
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(ONLY25)
file(GLOB_RECURSE SRCS source/*.cpp)
file(GLOB_RECURSE HDRS source/*.h)
set(EXECUTABLE_NAME "ONLY25")
include_directories("C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
set(CURL_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw")
set(CURL_LIBRARY "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.a" "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a")
set(CURL_INCLUDE_DIR "C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include")
find_package(CURL REQUIRED)
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
First, I use :
cmake -G "MinGW Makefiles" .
Everything works well
After that, I use :
C:\MinGW\bin\mingw32-make.exe
And I've got this linking error :
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x16): undefined reference to `_imp__curl_global_init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\ONLY25.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1d): undefined reference to `_imp__curl_global_cleanup'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\ONLY25.dir\build.make:106: recipe for target 'ONLY25.exe' failed
mingw32-make[2]: *** [ONLY25.exe] Error 1
CMakeFiles\Makefile2:93: recipe for target 'CMakeFiles/ONLY25.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/ONLY25.dir/all] Error 2
Makefile:101: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
I searched a lot on internet, there are some other posts on this topics but I think I did like they said.
I tried with different version of curl, and I'm using MinGW on Windows 10.
I'm really sorry if it's a dumb mistake, I still have a little trouble with Cmake. I'm stuck here for the whole day...
I'd appreciate your help with some explaination to not reproduce this aberration :)
I tried some new things thanks to responses :
set up FindCURL.cmake by copying this one link
change the CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
message(${CMAKE_MODULE_PATH})
set(CURL_LIBRARY "-lcurl")
find_package(CURL)
message(${CURL_LIBRARIES})
message(${CURL_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
add_executable(${EXECUTABLE_NAME} ${SRCS} ${HDRS})
target_link_libraries(${EXECUTABLE_NAME} ${CURL_LIBRARIES})
Currently, the cmake command is working well and the variables seems legit:
CURL_LIBRARIES : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.dll.aC:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/lib/libcurl.a
CURL_INCLUDE_DIRS : C:/Users/natha/Desktop/work/myLibs/curl-7.71.1-win64-mingw/include
However, there is again the linking error with the make command...
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)
I've been working on a project in CLion for quite some time now (over a month). All of a sudden, CLion will not build all of my files... When I run my program, my messages build looks like this:
Scanning dependencies of target main.cpp
[ 11%] Building CXX object CMakeFiles/main.cpp.dir/Custom_Types.cpp.o
[ 22%] Linking CXX executable ../bin/main.cpp
ld: library not found for -l*.cpp
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[3]: *** [../bin/main.cpp] Error 1
make[2]: *** [CMakeFiles/main.cpp.dir/all] Error 2
make[1]: *** [CMakeFiles/main.cpp.dir/rule] Error 2
make: *** [main.cpp] Error 2
Great... what the flippin fireworks is going on? This is what my CMake looks like...
cmake_minimum_required(VERSION 3.7)
project(Crazy_Sniper)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fpermissive")
find_package (OpenGL REQUIRED)
find_package (GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLUT_INCLUDE_DIRS})
file(GLOB SOURCE_FILES
*.cpp
*.h
)
add_executable(main.cpp ${SOURCE_FILES} Custom_Types.cpp)
target_link_libraries (main.cpp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES}
mdl pthread)
Does anyone know what the problem is? I've looked through countless forums, and I cannot seem to find someone with a similar problem. Any help would be much appreciated.
Update! I've managed to get CLion to build my files, but main still does nothing... I try simply printing something out, and the console does nothing. I get an error code 11 for some reason...?
Thank you!
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.
I have a project with the following structure
/cmake_modules/
FindSFML.cmake
/includes/
car.hpp
motor.hpp
tires.hpp
/sources/
car.cpp
motor.cpp
tires.cpp
/main.cpp
/main.hpp
I have the following CMakeFiles.txt file:
cmake_minimum_required(VERSION 2.8)
project (MYGAME)
set (MYGAME_VERSION_MAJOR 1)
set (MYGAME_VERSION_MINOR 0)
set (EXECUTABLE_NAME "mygame")
include_directories ("${MYGAME_BINARY_DIR}")
include_directories ("${MYGAME_BINARY_DIR}/includes")
link_directories ("${MYGAME_BINARY_DIR}/sources")
add_executable(${EXECUTABLE_NAME} main.cpp)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.0 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I try to execute make I get this:
[100%] Building CXX object CMakeFiles/mygame.dir/main.cpp.o
Linking CXX executable mygame
CMakeFiles/mygame.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'
main.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'
collect2: error: ld returned 1 exit status
make[2]: *** [mygame] Error 1
make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
make: *** [all] Error 2
How do I fix it?
You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.
You can either add them along with main.cpp in the executable directly:
set(MySources sources/car.cpp sources/motor.cpp sources/tires.cpp main.cpp)
add_executable(${EXECUTABLE_NAME} ${MySources})
or you can make these into a library and link that:
set(MyLibSources sources/car.cpp sources/motor.cpp sources/tires.cpp)
add_library(MyLib ${MyLibSources})
add_executable(${EXECUTABLE_NAME} main.cpp)
...
target_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})
A couple of other points to note:
You should avoid the use of link_directories if possible (its own documentation discourages its use), and it's often helpful to include the headers in the list of files added via add_executable or add_library since these then show up in IDEs like MS Visual Studio.