ITK compilation with cmake - c++

I tried to compile a simple helloWord C++ program with cmake using the ITK library. However I have some difficulties to link with the ITK library and my program doesn't work.
I put my program in the attachment.
helloWorld programme
CMakeLists.txt :
project(HelloWorld)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(HelloWorld HelloWorld.cxx)
target_link_libraries(HelloWorld ${ITK_LIBRAIRIES})
HelloWorld.cxx :
#include "itkImage.h"
#include <iostream>
int main()
{
typedef itk::Image< unsigned short, 3 > ImageType;
ImageType::Pointer image = ImageType::New();
std::cout <<"ITK Hello World !"<< std::endl;
return 0 ;
}

target_link_libraries(HelloWorld ${ITK_LIBRAIRIES})
should be
target_link_libraries(HelloWorld ${ITK_LIBRARIES})

Related

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)

Slient crash when calls to ffmpeg's libav exist

I have a fairly simple example.
#include <iostream>
extern "C"
{
#include <libavutil/opt.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
int main(int, char **)
{
AVFormatContext *format = 0; // avformat_alloc_context();
// avformat_open_input(&format, "http://s5radio.ponyvillelive.com:8026/stream.mp3", NULL, NULL);
// avformat_find_stream_info(format, NULL);
std::cout << "Hello, world!" << std::endl;
return 0;
}
I this outputs "Hello, world!" and I can set break points but the moment I uncomment anything calling avformat code the program silently closes with no error and no break points are hit making it impossible to debug.
An example would be changing AVFormatContext *format = 0; to AVFormatContext *format = avformat_alloc_context(); or uncommenting avformat_open_input(&format, "http://s5radio.ponyvillelive.com:8026/stream.mp3", NULL, NULL);. Without breakpoints or errors how do I solve?
Update:
This is what I get from the debugger:
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000139.
The program 'path\to\my\project\LibavPlayground.exe' has exited with code 0 (0x00000000).
Update2:
this is my cmake:
cmake_minimum_required(VERSION 3.0.0)
project(LibavPlayground VERSION 0.1.0 LANGUAGES CXX C)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBAV REQUIRED libavutil libavcodec libavformat)
include_directories(${LIBAV_INCLUDE_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_CONSTANT_MACROS -Wno-deprecated-declarations")
include_directories(include PUBLIC)
include_directories(src PRIVATE)
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(LibavPlayground ${SRC_FILES})
target_link_libraries(LibavPlayground ${LIBAV_LIBRARIES})
and all the libav* libraries were installed with a single pacman -S mingw-w64-x86_64-ffmpeg command
The current solution is to use WSL instead of MinGw as I don't duplicate the issue in WSL. Although I really hope someone out there has a real solution.

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

Clion (cmake) won't link the libraries [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm having a problem when trying to link SDL2 and GL libraries to Clion (cmake). Despite changing the cmakelists.txt and adding the target-link-libraries() function. So the cmake file looked like this:
cmake_minimum_required(VERSION 3.9)
project(OpenGL1)
set(CMAKE_CXX_STANDARD 17)
target_link_libraries(OpenGL1 SDL2 GL)
add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)
But with no luck. Clion would not link the libraries with errors such as undifined refference to functions of the libraries.
Then a friend suggested it that I would wite that into cmakelists file:
cmake_minimum_required(VERSION 3.9)
project(OpenGL1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lSDL2 -lOpenGL")
add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)
And it kinda worked... But it would not find the -lOpenGL.
Last but not least, my project will not compile with the traditional way in the terminal with g++. And it will compile fine with one .cpp file.
My project consists of 2 cpp files and a header file. Like that:
main.cpp
Display1.cpp
Display.h
And code of the files is the following:
main.cpp
#include <iostream>
#include "Display.h"
using namespace std;
int main() {
Display d1(800,600,"Hello");
while (!d1.m_isClosed()){
d1.Clear(0.0f, 0.15f, 0.3f, 1.0f);
d1.Update();
}
return 0;
}
Display1.cpp
#include "Display.h"
#include <GL/glew.h>
using namespace std;
Display::Display(int width, int height, const string title) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit();
if (status != GLEW_OK)
cerr << "Glew failed!" << endl;
isClosed = false;
}
Display::~Display() {
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
void Display::Clear(float r, float g, float b, float a) {
glClearColor(r,g,b,a);
glClear(GL_COLOR_BUFFER_BIT);
}
bool Display::m_isClosed() {
return isClosed;
}
void Display::Update() {
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT)
isClosed = true;
}
}
Display1.h
#ifndef OPENGL_DISPLAY_H
#define OPENGL_DISPLAY_H
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
class Display {
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool isClosed;
public:
Display(int width, int height, string title);
virtual ~Display();
void Update();
bool m_isClosed();
void Clear(float r, float g, float b, float a);
};
#endif //OPENGL_DISPLAY_H
Now, i know that others have asked the same, but nothing could help me so that's why I'm asking. I'm running Ubuntu 17.10 (64-bit). Thanks in advance!
You have to fix your CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(OpenGL1)
set(CMAKE_CXX_STANDARD 17)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
set(SDL2_INCLUDE_DIRS /usr/include/SDL2)
set(SDL2_LIBRARIES /usr/lib/x86_64-linux-gnu/libSDL2.so)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS})
add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)
target_link_libraries(OpenGL1 ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${SDL2_LIBRARIES})
First find the OpenGL files with find_package(OpenGL REQUIRED). Then set the paths for SDL2. Set the include directories for your target (OpenGL1). Add your target. Link libraries to your target.
A better way to use SDL2 is to use FindSDL2.cmake and add find_package(SDL2) to your CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(OpenGL1)
set(CMAKE_CXX_STANDARD 17)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(SDL2 REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS})
add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)
target_link_libraries(OpenGL1 ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${SDL2_LIBRARIES})
SDL2 installs its own cmake configuration file called sdl2-config.cmake, so you don't need to download it from github.

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.