Trouble with setting up SDL2_Image in CLion: undefined reference to `IMG_Init' - c++

I've been trying to setup the extension library SDL2_image for SDL2 on CLion (MingW). Steps for installation I've taken so far:
Copy the contents of SDL2_image development into C:\MingW
Created a FindSDL2_IMAGE.cmake file and linked it.
Here's my CMakeList.txt file:
cmake_minimum_required(VERSION 3.19)
project(TestGame)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
find_package(SDL2_IMAGE REQUIRED)
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
add_executable(TestGame src/main.cpp src/GameWindow.cpp src/GameWindow.h)
target_link_libraries(TestGame ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
And the relevant main.cpp file:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main(int argc, char *args[]) {
IMG_Init(IMG_INIT_PNG);
}
Edit: I've forgot to include the error message.
CMakeFiles\TestGame.dir/objects.a(main.cpp.obj): In function `Z4initv':
D:/_dev/C++/TestGame/src/main.cpp:14: undefined reference to `IMG_Init'

Related

CLion, MinGW and SDL2: Process finished with exit code -1073741515 (0xC0000135)

I am trying to add SDL2 to my CLion project. I found this guide and tried to follow it while including only SDL2. Everything compiles, but when I start my app I get "Process finished with exit code -1073741515 (0xC0000135)".
In my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-lmingw32 -static-libgcc -static-libstdc++")
set(SDL2_PATH "C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "C:/CPP/libs/CMakeModules")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
if (${SDL2_FOUND})
message(VERBOSE, "sdl found!")
else ()
message(FATAL_ERROR, "sdl not found")
endif ()
message(VERBOSE, ${SDL2_INCLUDE_DIR})
message(VERBOSE, ${SDL2_LIBRARY})
add_executable(Test src/main.cpp)
target_link_libraries(Test ${SDL2_LIBRARY})
main.cpp:
#include <SDL.h>
#include <cstdio>
int main(int argc, char *args[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
SDL_Quit();
return 0;
}
I am using CLion 2019.3.2 with bundled CMake, latest MinGW build (x86_64-8.1.0-win32-seh-rt_v6-rev0) and latest SDL2 (2.0.10).
CMake output also looks ok:
VERBOSE,sdl found!
VERBOSE,C:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/include/SDL2
VERBOSE,mingw32-mwindowsC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2main.aC:/CPP/libs/SDL2-2.0.10/x86_64-w64-mingw32/lib/libSDL2.dll.a-lpthread
if you are using visual studio toolchains in CLion:
You need to paste in folder cmake-build-debug or cmake-build-release the files .dll, but no only SDL2_image.dll, all files from folder lib/x86
SDL2_image-devel-2.0.5-VC.zip
My CMakeLists.txt seems different from yours.
Here are my configurations, Hope can help you.
cmake_minimum_required(VERSION 3.15)
project(cppSDL)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLIGS "${CMAKE_CXX_FLAGS} -std=c++17 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib/x86)
set(SOURCE_FILES SDLtutorial.cpp)
add_executable(SDLtutorial ${SOURCE_FILES})
target_link_libraries(SDLtutorial SDL2main SDL2 SDL2_ttf SDL2_image)

Linking SDL2 - CLion - Ubuntu 16.04 - G++

Background info
As the title says, I am on Ubuntu 16.04 using CLion and G++, and I am unable to link SDL2.
SDL2.h is found in the project at External Libraries/Header Search Paths/include/SDL2. This seems to link to /usr/include/SDL2.
So, with the header file found, I can #include <SDL2/SDL.h> without any issues. Yet, when I try to make use of SDL2 with something like, SDL_Init( SDL_INIT_EVERYTHING ), I get an undefined reference.
If I compile from terminal with g++ main.cpp -lSDL2 -o test I have no errors. But, if I compile from terminal with g++ main.cpp -o test, then I have the same error as CLion!
Question
How do I link SDL2 to Cmake? Did I add this flag -lsdl2 to the CMake file correctly? If I added it properly, then what am I missing?
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char* argv[]) {
SDL_Init( SDL_INIT_EVERYTHING );
std::cout << "Hello, World!" << std::endl;
SDL_Quit();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(untitled2)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lSDL2")
add_executable(untitled2 main.cpp)
You just need to search for the SDL2 package and link it to the target. Don't use CMAKE_CXX_FLAGS for this (or any sort of linking/header paths/etc); try the following:
find_package(SDL2 REQUIRED SDL2)
:
add_executable(untitled2 main.cpp)
target_link_libraries(untitled2
PRIVATE SDL2::SDL2
)
This will pull in the appropriate for headers etc. Note, this does not automatically include SDL_main or other libraries, which you may need or want in addition.
Additionally, for older versions of SDL2, SDL2::SDL2 may not work, and you may just want SDL2. I would recommend upgrading if this is the case, though.
You can try
SET(CMAKE_CXX_LINK_FLAGS "-lSDL2")
I modified the answer #rpav gave. This project has the minimal amount of code necessary to test that SDL2 was in fact working with CLion.
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char* argv[]) {
SDL_Init( SDL_INIT_EVERYTHING );
std::cout << "Hello, World!" << std::endl;
SDL_Quit();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(untitled2)
set(CMAKE_CXX_STANDARD 11)
find_package(SDL2 REQUIRED SDL2)
add_executable(untitled2 main.cpp)
target_link_libraries(untitled2 PRIVATE SDL2)
Most of the CMake file is provided by CLion. The only additional lines of code are find_package(SDL2 REQUIRED SDL2) and target_link_libraries(untitled2 PRIVATE SDL2). To clarify, untitled2, is the name of my project.

cmake: undefined reference to any pcap functions

I want to use pcap in my Clion project on linux.
I installed libpcap-dev:
sudo apt-get install libpcap-dev
But then I try to compile any file, containing pcap functions like:
#include <stdio.h>
#include <pcap.h>
int main(int argc, char *argv[])
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
printf("Device: %s\n", dev);
return(0);
}
I have cmake errors:
CMakeFiles/main.cpp.o: In function `main':
/main.cpp:8: undefined reference to `pcap_lookupdev'
I have not used cmake before. Cmake file:
cmake_minimum_required(VERSION 3.7)
project(mypr)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
You should include and use a FindPCAP.cmake file to your CMakeLists.txt. Here is one: https://github.com/bro/cmake/blob/master/FindPCAP.cmake
Put FindPCAP.cmake in your project's source directory and try changing your CMakeLists.txt to:
cmake_minimum_required(VERSION 3.7)
project(mypr)
set(CMAKE_CXX_STANDARD 11)
include(FindPCAP.cmake)
set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
target_link_libraries(mypr ${PCAP_LIBRARY})
I think you can also use CMAKE_MODULE_PATH (assume FindPCAP.cmake is in your project's source directory):
cmake_minimum_required(VERSION 3.7)
project(mypr)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/")
find_package(PCAP REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(mypr ${SOURCE_FILES})
target_link_libraries(mypr ${PCAP_LIBRARY})
I have encounted with a problem similar to this, when I tried to complied with libpcap by ros catkin_make
CMakeFiles/decode_pcap.dir/src/decode_pcap.cpp.o:在函数‘main’中:
decode_pcap.cpp:(.text+0x130):对‘pcap_open_live’未定义的引用
decode_pcap.cpp:(.text+0x1b3):对‘pcap_next’未定义的引用
decode_pcap.cpp:(.text+0x2a4):对‘pcap_loop’未定义的引用
decode_pcap.cpp:(.text+0x2e6):对‘pcap_close’未定义的引用
decode_pcap.cpp:(.text+0x2ff):对‘pcap_close’未定义的引用
collect2: error: ld returned 1 exit status
at last, I added the absolute path to "libpcap.so" by link_libraries() in CMakeLists.txt,like the below
link_libraries(/usr/lib/x86_64-linux-gnu/libpcap.so.0.8)

How do I connect static libs in CLion with CMake?

I want to use OpenGL 4.5 and freeglut in my project. I've got compiled freeglut library (include folder, .dll and .lib). Here's CMakeLists.txt I've got so far:
cmake_minimum_required(VERSION 3.7)
project(DuperTest)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/include})
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(DuperTest ${SOURCE_FILES})
target_link_libraries(DuperTest ${OPENGL_LIBRARIES})
target_link_libraries(DuperTest ${CMAKE_SOURCE_DIR}/lib/freeglut.lib)
But still no luck #include <gl.h> and #include <GL/freeglut.h> still not working. What should I do?

How to separate header file and source file in CMake?

My project is in the structure as follows:
--root: main.cpp CMakeLists.txt
--src: function.cpp CMakeLists.txt
--include: function.h
main.cpp:
#include <iostream>
#include "function.h"
using namespace std;
int main(int argc, char *argv[])
{
//call module in function.hpp
return 0;
}
CMakeLists.txt in root directory:
project(t1)
cmake_minimum_required(VERSION 2.8)
add_subdirectory(src)
file(GLOB_RECURSE SOURCES
include/function.h
src/function.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
CmakeLists.txt in src directory:
include_directories(${PROJECT_SOURCE_DIR}/include)
How to write CMakelists in the root and src directory to realize separate implementation of function? And further more, how to call them in main. Possible solutions in CMake not finding proper header/include files in include_directories. But it still doesn't match my situations.
in root:
project(t1)
cmake_minimum_required(VERSION 2.8)
include_directories(include)
add_subdirectory(src)
in src:
set(TARGET target_name)
add_executable(${TARGET} main.cpp function.cpp)
Today, I have the same question and I found the solution.
CMakeLists.txt in root directory:
cmake_minimum_required(VERSION 3.21.3)
project(t1)
add_executable(${PROJECT_NAME} main.cpp)
add_subdirectory(src)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_link_directories(${PROJECT_NAME} PRIVATE src)
target_link_libraries(${PROJECT_NAME} function)
CMakeLists.txt in src directory:
include_directories(../include)
add_library(function function.cpp)
Thanks Code, Tech and Tutorial and his video https://www.youtube.com/watch?v=kEGQKzhciKc&t=606s. This helps me to have an idea to solve this problem