C++ boost::regex_match gives all NULLs results - c++

I am facing a weird problem which is boost::regex_match gives all NULLs results.
Sorry for the bad problem description. Let me copy and paste the code below.
I think the boost lib version should be Boost 1-47-0. GCC 4.3.2 on Linux.
#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main()
{
string aFreeText = "26JAN07";
boost::regex expression("([0-9]{2}[A-Z]{3}[0-9]{2})");
boost::smatch results;
if(boost::regex_match(aFreeText, results, expression))
{
for(int index=0; index<results.size(); index++)
DEBUG("YI JI results[" << index << "].str(): " << results[index].str());
}
return 0;
}
However, in the log, the display is very weird.
Can anyone kindly help me with this problem? Your kind help will be greatly appreciated.
If the provided information is not enough, please feel free to leave your comment and I will add them later.

Your code works fine with Boost 1.55, GCC 4.9.2 on Windows 7 (64 Bits)
It could be a problem with your boost installation
The code below produces the following result (as you expected)
$ ./re.exe
Results[0].str(): 26JAN07
C++ code: re.cpp
#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main()
{
string aFreeText = "26JAN07";
boost::regex expression("[0-9]{2}[A-Z]{3}[0-9]{2}");
boost::smatch results;
if(boost::regex_match(aFreeText, results, expression))
{
for(size_t index=0; index<results.size(); ++index)
{
std::cout<<"Results[" << index << "].str(): " << results[index].str()<<std::endl;
}
}
return 0;
}
The CMakeLists.txt used looks like
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
PROJECT(RE)
#############################################################################
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREADED OFF)
FIND_PACKAGE(Boost 1.53 COMPONENTS regex REQUIRED)
#############################################################################
IF (NOT(MSVC))
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
ENDIF()
ADD_EXECUTABLE(re re.cpp)
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(re ${Boost_LIBRARIES})

Related

log4cplus ConsoleAppender does not work in container

I'm trying to containerize my application but I'm having trouble getting log4cplus working. Bottom line up front, logging works when running on the host, but not in the container when I'm logging from long running loops. Its seems like a buffer somewhere is not getting flushed. A minimal example follows.
Additionally, removing the long lived loop removes the issue, presumably log4cplus flushes one last time before tearing down. Lengthening the sleep duration did not seem to help.
main.cpp
#include <iostream>
#include <unistd.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <log4cplus/initializer.h>
int main(int argc, char **argv)
{
std::cout << "Sanity Check" << std::endl;
auto log4cplus = ::log4cplus::Initializer();
std::string logconfig("log4cplus_configure.ini");
::log4cplus::PropertyConfigurator::doConfigure(logconfig);
auto logger = ::log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main"));
while (true) {
LOG4CPLUS_ERROR(logger, "Sleeping...");
// std::cout << "cout sleep..." << std::endl; // uncomment to get log messages working
sleep(1);
}
return 0;
}
log4cplus_configure.ini
log4cplus.rootLogger=INFO, MyConsoleAppender
log4cplus.appender.MyConsoleAppender=log4cplus::ConsoleAppender
log4cplus.appender.MyConsoleAppender.layout=log4cplus::PatternLayout
log4cplus.appender.MyConsoleAppender.layout.ConversionPattern=[%-5p][%d] %m%n
Dockerfile
FROM rockylinux:latest
RUN dnf install -y boost-system
COPY ./build/sandbox /
COPY ./log4cplus_configure.ini /
CMD ["/sandbox"]
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_STANDARD 17)
# Project executable and library
add_executable(sandbox main.cpp)
target_link_libraries(sandbox
PUBLIC liblog4cplus.a
PUBLIC pthread
PUBLIC boost_system
)
Not sure why, but adding log4cplus.appender.MyConsoleAppender.ImmediateFlush=true to log4cplus_configure.ini solved my issue.

Why won't music play using SDL

#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)

How to mix dynamic and static boost libraries in one cmake project?

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.

Undefined references for aruco::MarkerDetector

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;
}

allegro not working with clion (0xC000007B)

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.