Using SDL2 with CLion on OS X - c++

So I am trying to get SDL2 to work with CLion (So that I can experiment/learn).
My main code is as such:
#include <iostream>
#include <SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool init();
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
bool success = true;
/*if(SDL_Init(SDL_INIT_VIDEO)<0){
success = false;
}
else{
}*/
return success;
}
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
And my CMake file looks like this
cmake_minimum_required(VERSION 3.6)
project(SDL2_Lesson_1)
set(CMAKE_CXX_STANDARD 11)
# includes cmake/FindSDL2.cmake
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
set(SOURCE_FILES Lesson_1.cpp)
add_executable(SDL2_App ${SOURCE_FILES})
target_link_libraries(SDL2_App ${SDL2_LIBRARY})
set(SOURCE_FILES Lesson_1.cpp)
add_executable(SDL2_Lesson_1 ${SOURCE_FILES})
Also, I have a file FindSDL2.cmake from here in a folder cmake within the project folder.
Now, with the files as I have them posted, everything compiles and runs fine. BUT, when I uncomment the commented section within init(), the compilation breaks down and gives me the following error:
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
init() in Lesson_1.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [SDL2_Lesson_1] Error 1
make[2]: *** [CMakeFiles/SDL2_Lesson_1.dir/all] Error 2
make[1]: *** [CMakeFiles/SDL2_Lesson_1.dir/rule] Error 2
make: *** [SDL2_Lesson_1] Error 2
Note: Lesson_1.cpp is the file with the main code. Also, this is only part of the error.

Use find_library() instead of find_package()
find_library(SDL2_LIBRARY SDL2 "path/to/your/library_bundle")
find_library(SDL2_App ${SDL2_LIBRARY})

Related

error when I trying to make my C++ project using CPR and TorchScript Library

I am trying to use CPR to get Post Request from an API but when I make my project using Cmake, it always error. Let's have a look at my CMakeList.txt, code and error.
Any ideas are very good to me. Thank in advance my bros.
CMakeList.txt
cmake_minimum_required(VERSION 3.15)
project(Preprocessing)
add_subdirectory(cpr)
add_executable(Preprocessing main.cpp)
set(CMAKE_PREFIX_PATH "/home/bao/CLionProjects/Preprocessing/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
target_link_libraries(Preprocessing ${CPR_LIBRARIES} ${TORCH_LIBRARIES})
include_directories(${CPR_INCLUDE_DIRS})
set_property(TARGET Preprocessing PROPERTY CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} ${CPR_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
my code:
void query_split_text_service (std::string s){
json j;
j["sentence"] = s;
std::cout<< j << std::endl;
auto r = cpr::Post(cpr::Url{"http://127.0.0.1:8880/nlp/tone"},
cpr::Body{j.dump()}
);
std::cout << r.text << std::endl;
}
and error:
[ 78%] Linking CXX executable Preprocessing
CMakeFiles/Preprocessing.dir/main.cpp.o: In function `void cpr::priv::set_option<std::string>(cpr::Session&, std::string&&)':
main.cpp:(.text._ZN3cpr4priv10set_optionISsEEvRNS_7SessionEOT_[_ZN3cpr4priv10set_optionISsEEvRNS_7SessionEOT_]+0x2a): undefined reference to `cpr::Session::SetOption(std::string const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/Preprocessing.dir/build.make:91: recipe for target 'Preprocessing' failed
make[2]: *** [Preprocessing] Error 1
CMakeFiles/Makefile2:346: recipe for target 'CMakeFiles/Preprocessing.dir/all' failed
make[1]: *** [CMakeFiles/Preprocessing.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
but If comment some lines, the miracle that I can make my project and run it.
void query_split_text_service (std::string s){
json j;
j["sentence"] = s;
std::cout<< j << std::endl;
// auto r = cpr::Post(cpr::Url{"http://127.0.0.1:8880/nlp/tone"},
// cpr::Body{j.dump()}
// );
// std::cout << r.text << std::endl;
}

Linking local library in c++ (macOS High Sierra)

I'm trying to link a library called apriltags to my c++ project. I've downloaded the source files, placed them in my project and set up the CMakeLists.txt such that the imports in my classes work fine. However, there is a referencing problem, such that I get the following error when using building with:
$ /Applications/CLion.app/Contents/bin/cmake/bin/cmake --build "/Users/petter/Desktop/MSc Project/autonomous-car/cmake-build-debug" --
target autonomous_car -- -j 2
Error:
[ 50%] Linking CXX executable bin/autonomous_car
Undefined symbols for architecture x86_64:
"AprilTags::TagDetector::extractTags(cv::Mat const&)", referenced from:
_main in main.cpp.o
"AprilTags::TagFamily::TagFamily(AprilTags::TagCodes const&)", referenced from:
AprilTags::TagDetector::TagDetector(AprilTags::TagCodes const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [bin/autonomous_car] Error 1
make[2]: *** [CMakeFiles/autonomous_car.dir/all] Error 2
make[1]: *** [CMakeFiles/autonomous_car.dir/rule] Error 2
make: *** [autonomous_car] Error 2
Here is my code:
#include <iostream>
#include <cstring>
#include <vector>
#include <sys/time.h>
#include <AprilTags/TagDetector.h>
#include <AprilTags/Tag36h11.h>
#include <eigen3/Eigen/Dense>
using namespace AprilTags;
using namespace cv;
int main() {
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("view", 1);
TagCodes tag_codes = tagCodes36h11;
TagDetector* detector = NULL;
detector = new TagDetector(tag_codes);
for (;;) {
Mat frame;
cap >> frame; // get a new frame from camera
vector<TagDetection> det = detector->extractTags(frame);
imshow("view", frame);
if (waitKey(30) >= 0) break;
}
return 0;
}`
And here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(autonomous_car)
set(SOURCE_FILES main.cpp )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_executable(autonomous_car ${SOURCE_FILES})
add_library(apriltags ${SOURCE_FILES})
find_package(OpenCV REQUIRED)
find_package(Sdl2 REQUIRED)
find_package(PkgConfig)
pkg_check_modules(EIGEN3 REQUIRED eigen3)
include_directories(${EIGEN3_INCLUDE_DIRS})
target_link_libraries(apriltags)
target_link_libraries(autonomous_car ${OpenCV_LIBS})
target_link_libraries(autonomous_car ${Eigen_LIBS})
target_link_libraries(autonomous_car ${Sdl2_LIBS})
include_directories(apriltags . )
include_directories(${Eigen_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${Sdl2_INCLUDE_DIRS})
Project structure:
project structure

Clion Cmake & SDL2

I want to use Clion with SDL2 and after lot of tests, I already have an error.
there is my CmakeLists.txt :
cmake_minimum_required(VERSION 3.8)
project(sdl2-test)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
#set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp)
add_executable(sdl2-test ${SOURCE_FILES})
target_link_libraries(sdl2-test SDL2main SDL2)
my main.cpp
#include <iostream>
#include "include/SDL2/SDL.h"
using namespace std;
int main(int argc, char** argv) {
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
cout << "Something went wrong! " << SDL_GetError() << endl;
}
SDL_Window* window = SDL_CreateWindow("SDL_Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, SDL_WINDOW_OPENGL);
if(window == nullptr){
cout << "Something also went wrong here" << endl;
}
SDL_Delay(2000);
SDL_Quit();
return 0;
}
but when I build my project cmake Obviously Cmake work find but I haave some error :
C:\Users\paulp\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\172.4343.16 \bin\cmake\bin\cmake.exe --build C:\Users\paulp\CLionProjects\sdl2-test\cmake- build-debug --target sdl2-test -- -j 4
[ 50%] Building CXX object CMakeFiles/sdl2-test.dir/main.cpp.obj
[100%] Linking CXX executable sdl2-test.exe
CMakeFiles\sdl2-test.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:8: undefined reference to `SDL_Init'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:9: undefined reference to `SDL_GetError'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:13: undefined reference to `SDL_CreateWindow'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:18: undefined reference to `SDL_Delay'
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:19: undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\sdl2-test.dir\build.make:96: recipe for target 'sdl2-test.exe' failed
mingw32-make.exe[3]: *** [sdl2-test.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/sdl2-test.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/sdl2-test.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/sdl2-test.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/sdl2-test.dir/rule] Error 2
mingw32-make.exe: *** [sdl2-test] Error 2
Makefile:117: recipe for target 'sdl2-test' failed
any idea how to fix ?
The linking of your application has failed because the linker does not know the location of the sdl2-lib.
You need to search for the SDL2-libraries as well in your cmake-file and add the locations to your makefile:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(ChickenShooter main.cpp)
target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR})
target_link_libraries(ChickenShooter ${SDL2_LIBRARY})
You can look into this post as well: How to dins SDL2 via cmake

CLion: undefined "_get_driver_instance"

The IDE : CLion
System: OS X
Error messageļ¼š
Scanning dependencies of target librarySystem
[ 66%] Building CXX object CMakeFiles/librarySystem.dir/sqlConnection.cpp.o
[ 66%] Building CXX object CMakeFiles/librarySystem.dir/main.cpp.o
[100%] Linking CXX executable librarySystem
Undefined symbols for architecture x86_64:
"_get_driver_instance", referenced from:
sqlConnection::sqlConnection() in sqlConnection.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [librarySystem] Error 1
make[1]: *** [CMakeFiles/librarySystem.dir/all] Error 2
make: *** [all] Error 2
I write a class named sqlConnection to connect mysql.
sqlConection.h
#include "sqlConnection.h"
sqlConnection::sqlConnection() {
driver = get_driver_instance();
con = driver->connect("567aaffa1a70e.sh.cdb.myqcloud.com:xxxx", "xxxx", "xxxx");
con->setSchema("librarySys");
stmt = con->createStatement();
}
bool sqlConnection::ifConnected() {
bool isConnected = false;
if(!con->isClosed()){
std::cout << "Succeed to connect mysql";
isConnected = true;
}else{
std::cout << "fail to connect mysql";
}
return isConnected;
}
sqlConnection::~sqlConnection() {
delete stmt;
delete con;
}
The test in the main.cpp
main.cpp
#include <iostream>
#include "sqlConnection.h"
using namespace std;
int main() {
sqlConnection *sqlC = new sqlConnection();
sqlC->ifConnected();
return 0;
}
cmakeList:
cmake_minimum_required(VERSION 3.3)
project(librarySystem)
INCLUDE_DIRECTORIES(sqlFiles/include)
INCLUDE_DIRECTORIES(sqlFiles/lib)
INCLUDE_DIRECTORIES(sqlFiles/include/cppconn)
INCLUDE_DIRECTORIES(/usr/local/lib/libmysqlcppconn.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x")
set(SOURCE_FILES main.cpp sqlConnection.cpp sqlConnection.h)
add_executable(librarySystem ${SOURCE_FILES})
I used mysql connector-cpp to connect mysql.But the problem came.Have tried the solution on web,but they did't work.
Struggled with the same error and I got a small example to work with this CMakeLists.txt content. Maybe it's helpful for you even if its not same versions.
cmake_minimum_required(VERSION 3.5)
project(TestCPP)
#For mysql connector include..
INCLUDE_DIRECTORIES(/mypath/mysql-connector-c++-1.1.7-osx10.10-x86-64bit/include/)
#For Boost..
INCLUDE_DIRECTORIES(/opt/local/include/)
#For imported linking..
add_library(libmysqlcppconn STATIC IMPORTED)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION /mypath/mysql-connector-c++-1.1.7-osx10.10-x86-64bit/lib/libmysqlcppconn-static.a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(TestCPP ${SOURCE_FILES})
target_link_libraries (TestCPP libmysqlcppconn)

Old OPENGL code, want to use glPushMatrix()

1st Question!
I'm trying to compile some openGL code where I use glPushMatrix().
I plan on reformatting the code to more recent practices (because glPushMatrix is deprecated), but,for now, I'd like to use a version of (openGL, GLU, GLUT)? where this function works.
My current cmake file looks like this
#CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (PROJECTOR)
set( PROJECTOR_VERSION_MAJOR 0)
set( PROJECTOR_VERSION_MINOR 1)
add_executable( Projector.run /*...buncha files */ )
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
find_package(ImageMagick COMPONENTS Magick++)
include_directories(${ImageMagick_INCLUDE_DIRS})
# TODO - this doesn't rollback versions. It just sets a min version requirement.
# I'd like to roll back the versions.
find_package(OpenGL 2.0)
include_directories(${OpenGL_INCLUDE_DIRS})
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
find_package(GLU)
include_directories(${GLUE_INCLUDE_DIRS})
target_link_libraries(Projector.run ${ImageMagick_LIBRARIES})
target_link_libraries(Projector.run ${Boost_LIBRARIES})
target_link_libraries(Projector.run ${GLU_LIBRARIES})
target_link_libraries(Projector.run ${OpenGL_LIBRARIES})
target_link_libraries(Projector.run ${GLUT_LIBRARIES})
#
And this is the error output:
/usr/bin/ld: CMakeFiles/Projector.run.dir/open_image.cpp.o: undefined reference to symbol 'glPushMatrix'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [Projector.run] Error 1
make[1]: *** [CMakeFiles/Projector.run.dir/all] Error 2
make: *** [all] Error 2
And this is a code snippet:
...
glPushMatrix();
int img_height,img_width = 3;
int row,col = 0;
for(col = 0; col < img_height; col++ )
{
for(row = 0; row < img_width; row++){
glTranslatef(CUBE_DIMEN,0.0f,0.0f);
glColor3f(r_comp,g_comp,b_comp);
glutSolidCube(CUBE_DIMEN);
}
glTranslatef(-1 * img_width * CUBE_DIMEN, -CUBE_DIMEN, 0.0f);
}
glPopMatrix();
...
What should I add so that my program will build?
Compile using the -lGL flag to include the library:
g++ -o main main.cpp -Wall -std=c++11 -lglut -lGL