I'm trying to link my C++ program with static Boost::program_options and dynamic Boost::log libraries.
Here is my CMakeList.txt file:
project(test)
add_executable(testprog "main.cpp")
add_definitions(-DBOOST_ALL_NO_LIB)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS program_options)
target_link_libraries(testprog PRIVATE Boost::program_options)
set(Boost_USE_STATIC_LIBS OFF)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package(Boost COMPONENTS log)
target_link_libraries(testprog PRIVATE Boost::log)
and my main.cpp file:
#define BOOST_ALL_NO_LIB
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
int main(int argc, char* argv[])
{
namespace po = boost::program_options;
po::options_description desc("allowed arguments");
desc.add_options()("log", po::value<std::string>(), "log level");
po::variables_map options;
po::store(po::command_line_parser(argc, argv).options(desc).run(), options);
po::notify(options);
if (options.count("log")) {
std::cout << "log level: " << options["log"].as<std::string>() << std::endl;
}
BOOST_LOG_TRIVIAL(info) << "test message";
return 0;
}
I'm getting boost_filesystem.dll is missing from your computer error.
If I get rid of set(Boost_USE_STATIC_LIBS OFF) line, I'm getting unresolved external symbols boost::log.... Looks like Boost_USE_STATIC_LIBS can be set only once during the configuration time, but I just cannot find the way to tell cmake to split static/dynamic libraries...
Thanks.
Related
I downloaded SDL2 release edition from: sdl-github: SDL2-devel-2.24.0-mingw.zip.
The OS is windows 10.
Compile environment is MINGW64.
IDE is clion.
My CMakeLists.txt file is
cmake_minimum_required(VERSION 3.20)
project(sdl_test)
set(CMAKE_CXX_STANDARD 20)
list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2-2.24.0")
MESSAGE(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(sdl2 CONFIG REQUIRED)
MESSAGE(STATUS "SDL2_INCLUDE_DIRS is ${SDL2_INCLUDE_DIRS}")
MESSAGE(STATUS "SDL2_LIBRARIES is ${SDL2_LIBRARIES}")
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(sdl_test main.cpp)
target_link_libraries(sdl_test ${SDL2_LIBRARIES}-static)
target_link_libraries(sdl_test ${SDL2_LIBRARIES}main)
The main.cpp file is:
extern "C"{
#include <SDL.h>
};
#include <stdio.h>
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
printf("SDL initialize ok!");
SDL_Quit();
getchar();
return 0;
}
The program can be compiled with no errors but when running it turns that:
runnerw.exe: CreateProcess failed with error 193: %1
#include<iostream>
#include <SDL_mixer.h>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_AUDIO);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
cout << "Error: " << Mix_GetError() << std::endl;
}
Mix_Music* song = Mix_LoadMUS("sample.wav");
Mix_PlayMusic(song, 1);
Mix_FreeMusic(song);
song = nullptr;
Mix_Quit();
SDL_Quit();
return 0;
}
I am trying to play a simple song, that I converted to WAV. It is located within the program's folder. I didn't get any errors/warnings when compling and the libraries seem to be linked correctly. On failure, loadMUS returns NULL (which isn't the case) and PlayMusic returns -1 upon failure (which again isn't the case). Yet, when I run the program nothing is heard. This is my first time working with SDL.
To just play a wave file this sample will work with SDL2:
#include <SDL.h>
#include <SDL_audio.h>
int main() {
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec spec{};
Uint8 *audio_buf{};
Uint32 audio_len{};
SDL_LoadWAV("path_to.wav", &spec, &audio_buf, &audio_len);
SDL_AudioSpec ob_spec{};
auto device = SDL_OpenAudioDevice(NULL, 0, &spec, &ob_spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
SDL_QueueAudio(device, audio_buf, audio_len);
SDL_PauseAudioDevice(device, 0);
SDL_Delay(5000);
SDL_CloseAudioDevice(device);
SDL_FreeWAV(audio_buf);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
BTW to link easy to SDL2 just use CMake with
cmake_minimum_required(VERSION 3.19)
project(sample_sdl VERSION 1.0)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2 SDL2::SDL2main)
I'm trying to get the marker position in an image, but when I compile the program, during linking, I receive several errors indicating undefined reference to Aruco (Marker.cpp:(.text+0x1643): undefined reference to 'aruco::MarkerDetector::detect(cv::Mat const&, std::vector<aruco::Marker, std::allocator<aruco::Marker> >&, cv::Mat, cv::Mat, float, bool)'), I've looked for solutions regarding linking the library but none of them seem to work. This is my CMakeList.txt:
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(Pruebas_alg_new)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
find_package(aruco)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(Marker Marker.cpp)
target_link_libraries(Marker PRIVATE ${OpenCV_LIBS})
And here is the code I'm trying to compile:
#include <iostream>
#include <stdio.h>
#include <opencv2/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgcodecs.hpp>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "Euler.hpp"
#include <aruco/aruco.h>
#include <aruco/markerdetector.h>
#include <aruco/marker.h>
int main(int argc, char **argv)
{
Mat img_scene = imread("/home/javier/pruebas/MasPruebas/Imagenes/Scene.jpg", IMREAD_GRAYSCALE );
if( /*!img_object.data ||*/ !img_scene.data )
{ std::cout<< " --(!) Error reading images " << std::endl;}
Mat CameraMatrix = (Mat_<double>(3,3) << 374.67,0,320.5,0,374.67,180.5,0,0,1);
aruco::MarkerDetector MDetector;
vector<aruco::Marker> Markers;
//DetectorParameters::create()
MDetector.detect(img_scene,Markers);
std::cout<< Markers.size() << std::endl;
// if at least one marker detected
for (unsigned int i = 0; i<Markers.size(); i++)
{Markers[i].draw(img_scene,cv::Scalar(0,0,255),2);}
cv::imshow("out", img_scene);
cv::waitKey(0);
return 0;
}
so I've hit a dead end, I'm simplying trying to run the following:
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
int main(int argc, char **argv) {
std::cout << "Hello, World!" << std::endl;
al_init();
std::cout << "hi" << std::endl;
return 0;
}
CMakeList.txt:
cmake_minimum_required(VERSION 3.6)
project(allegro_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
SET(ALLEGRO_ROOT C:/Users/duck-/workspace/cpp/recources/allegro/)
INCLUDE_DIRECTORIES( ${ALLEGRO_ROOT}/include )
LINK_DIRECTORIES( ${ALLEGRO_ROOT}/lib)
add_executable(allegro_test ${SOURCE_FILES})
TARGET_INCLUDE_DIRECTORIES(allegro_test PUBLIC ${ALLEGRO_ROOT})
TARGET_LINK_LIBRARIES(allegro_test allegro)
output:
"C:\Users\duck-\.CLion2016.2\system\cmake\generated\allegro test-50fbd97d\50fbd97d\Debug\allegro_test.exe"
Process finished with exit code -1073741701 (0xC000007B)
I'm using allegro-mingw-gcc6.2.0-x64-dynamic-5.2.2.zip from here and have copyied allegro-5.2.dll, cygintl-1.dll & cygintl-2.dll to
C:\Users\duck-.CLion2016.2\system\cmake\generated\allegro test-50fbd97d\50fbd97d\Debug\
where the program's executed. I've installed .NET Framework 4.6.2. Am I missing something? cheers in advance.
Since you are building the program in a Debug configuration, you'll need to include allegro-debug-5.2.dll, and not just allegro-5.2.dll.
I am trying to learn C++ and I am having a bit of nightmare doing a test where I connect to a MySQL database.
I've had issues with the MySQL connector not linking properly then was getting issues related to relocation truncated to fitr_x86_64_32 against symbol.
I think I have fixed that by adding a compiler flag and now the app successfully builds and links.
When I run the app, it gets as far as calling get_driver_instance but then it exits. No exception is thrown, no errors nothing just exit code 0.
Below is my DBManager class
#include "DBConnectionManager.h"
using namespace std;
DBConnectionManager::DBConnectionManager() {
cout << "Starting DBConnectionManager - Updated" << endl;
try {
cout << "Getting driver instance" << endl;
driver = get_driver_instance();
cout << "Got driver instance" << endl;
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
conn->setSchema("bugs");
cout << "Connected to database" << endl;
}
catch (SQLException ex) {
cout << "Error connecting to DB: " << ex.what() << endl;
}
catch (...) {
cout << "Something has gone wrong" << endl;
}
}
Below is the header file
#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H
#define MYSQLTEST_DBCONNECTIONMANAGER_H
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
using namespace sql;
class DBConnectionManager
{
private:
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *statement;
sql::ResultSet *res;
public:
DBConnectionManager();
void performSql();
};
#endif //MYSQLTEST_DBCONNECTIONMANAGER_H
Below is my main method
#include "DBConnectionManager.h"
int main() {
DBConnectionManager dbConnectionManager;
dbConnectionManager.performSql();
return 0;
}
Below is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
include_directories("C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\include\\cppconn" "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt")
SET(GCC_COVERAGE_LINK_FLAGS "-m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread ")
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
add_library(mysqlcppconn.lib)
set_target_properties(MySQLTest PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(mysqlcppconn.lib PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(MySQLTest "C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.7\\lib\\opt\\mysqlcppconn.lib")
When I create the instance of my DBConnectionManager class it successfully calls the query and prints Starting DBConnectionManager - Updated followed by Getting Driver Instance but then it exits with Process finished with exit code 0 with no clues as to what went wrong.
Update
I'm finally getting somewhere. I found there are some MySQL client libraries within Cygwin so I have download them and referenced them in the cmake file.
My cmake file now looks like this:
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
SET(CPPCONN_PUBLIC_FUNC=)
SET(GCC_COVERAGE_LINK_FLAGS "-g -m64 -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -lpthread -pthread -Wl,--image-base -Wl,0x10000000 -lz")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCPPCONN_PUBLIC_FUNC= -Dmysqlcppconn_EXPORTS -std=c++11 -g -m64 -Wl,--image-base -Wl,0x10000000 -lpthread -pthread -lz")
include_directories("C:/mysql_connector/include")
include_directories("C:/boost_1_61_0")
set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
find_package(Boost COMPONENTS REQUIRED)
link_directories(C:/mysql_connector/lib)
target_link_libraries(MySQLTest "C:/mysql_connector/lib/mysqlcppconn.dll" "C:/Program Files/MySQL/MySQL Server 5.7/lib/libmysql.dll" "C:/mysql_connector/lib/libmysqlclient.dll.a" "C:/mysql_connector/lib/libmysqlclient_r.dll.a" ${Boost_LIBRARY_DIR})
Notice how I have linked the libraries libmysqlclient.dll.a and libmysqlclient_r.dll.a which is what I got from Cygwin.
When I run the app now it successfully gets the driver instance and to the console is outputted
Starting DBConnectionManaged - Updated
Getting driver instance
Got driver instance
But when I try and connect with driver->connect I then get the following error
0 [main] MySQLTest 2976 C:\Users\Chris\.CLion2016.2\system\cmake\generated\MySQLTest-8702ae13\8702ae13\Debug\MySQLTest.exe: *** fatal error - Internal error: TP_NUM_C_BUFS too small: 50
When I put it through the debugger, it fails on the driver->connect with
gdb: unknown target exception 0xe06d7363 at 0x7fff11347788
Program received signal ?, Unknown signal.
0x00007fff11347788 in RaiseException () from /cygdrive/c/WINDOWS/System32/KERNELBASE.dll
Update 2
Everything I've read points the mysql connector binaries should work fine, so I started again. Below is now the contents of my cmake file
cmake_minimum_required(VERSION 3.6)
project(MySQLTest)
#add_compile_options("-v")
SET(GCC_COVERAGE_LINK_FLAGS )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_INCLUDE_DIR C:/boost_1_61_0)
set(BOOST_LIBRARY_DIR C:/boost_1_61_0/libs)
include_directories("C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include" "C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/include/cppconn" ${BOOST_INCLUDE_DIR})
set(SOURCE_FILES main.cpp DBConnectionManager.cpp)
add_executable(MySQLTest ${SOURCE_FILES})
find_package(Boost COMPONENTS REQUIRED)
link_directories(C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt)
target_link_libraries(MySQLTest C:/Program\ Files/MySQL/MySQL\ Connector\ C++\ 1.1.7/lib/opt/mysqlcppconn.lib ${Boost_LIBRARY_DIR})
Now when I compile I get the original error
C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol `__imp_get_driver_instance' defined in .idata$5 section in C:/Program Files/MySQL/MySQL Connector C++ 1.1.7/lib/opt/mysqlcppconn.lib(mysqlcppconn.dll.b)
That sounds like to me like my app is compiling as 32 bit instead of 64 bit. As a test I ran the following code:
cout << "Int size is: " << sizeof(int) << endl;
The code above prints 4 (shouldn't it be 8 if it was compiled as 64 bit).
If my thinking is correct, why isn't it compiling it as 64 bit, I've tried setting the compiler flag -m64 but makes no difference. I've installed the Cygwinx64 as well which CLion is using.
You can use MySQL Connector C++
This is how to configure using Cmake
cmake_minimum_required(VERSION 3.7)
project(projectname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Include the dirs where you extracted the mysql-connector-cpp - download
include_directories(/usr/local/include/mysql-connector-cpp/include)
Create a cmake variable containing the project source files
set(SOURCE_FILES main.cpp)
Create your executable
add_executable(projectname ${SOURCE_FILES})
Link after creating executable
target_link_libraries(projectname mysqlcppconn)
Your CMakeList should at least have this or look like this in this order
cmake_minimum_required(VERSION 3.7)
project(projectname)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(/usr/local/include/mysql-connector-cpp/include)
set(SOURCE_FILES main.cpp)
add_executable(projectname ${SOURCE_FILES})
target_link_libraries(projectname mysqlcppconn)
You can use mysql++ library to connect to mysql from c++.
For that install mysql++,
(Ubuntu)
sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqlclient-dev libmysql++-dev libmysqlcppconn-dev
(Mac)
brew install mysql++
include library by adding,
/usr/include/mysql++
/usr/include/mysql
and add linkers,
-lmysqlpp -lmysqlclient
Example code,
#include <iostream>
#include <string>
#include <mysql++.h>
#include <mysql.h>
#define dbname "dbname"
#define server "localhost"
#define user "username"
#define pass "password"
using namespace std;
using namespace mysqlpp;
int main() {
Connection con(true);
try {
con.connect(dbname, server, user, pass);
cout << "Connected to database\n";
string s = "SELECT * FROM mirrors_mee WHERE id=1";
Query q = con.query(s);
StoreQueryResult sq = q.store();
StoreQueryResult::iterator it;
it = sq.begin();
while (it != sq.end()) {
Row row = *it;
cout << row[5] << " " << row[6] << " " << row[7] << endl;
it++;
}
} catch (Exception &e) {
cout << e.what() << endl;
}
return 0;
}
You will get full documentation of the library here.