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
Related
I'm programming in C. I want use ImageMagick library but some fuction can't be resolved.
This is my cMakeList.txt file:
cmake_minimum_required(VERSION 3.3)
project(WebServer)
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(SOURCE_FILES io.c server.c lock_fcntl.c sig_handler.c thread_job.c msg_parser.c)
set(LIB http-parser-master/http_parser.c )
set(CMAKE_USE_PTHREADS_INIT true)
set(CMAKE_USE_PTHREADS_INIT ON)
find_package(Threads REQUIRED)
find_package(ImageMagick COMPONENTS ImageWand)
include_directories(header)
include_directories(http-parser-master)
#include_directories(/usr/local/include/ImageMagick-7/MagickWand)
include_directories(${ImageMagick_INCLUDE_DIRS})
add_executable(server ${SOURCE_FILES} ${LIB})
add_executable(client client.c io.c)
add_executable(main main.c io.c)
target_link_libraries(main ${ImageMagick_LIBRARIES})
target_link_libraries(server Threads::Threads)
and this is the source main.c:
#include <ImageMagick-7/MagickWand/MagickWand.h>
#include "basic.h"
void convert_image(char *path, float quality_factor, char *out) {
int width, height;
MagickWand *n_wand = NULL;
MagickWandGenesis();
m_wand = (struct MagickWand *) NewMagickWand();
MagickReadImage(m_wand,"logo:");
width = MagickGetImageWidth(m_wand);
height = MagickGetImageHeight(m_wand);
if((width /= 2) < 1)width = 1;
if((height /= 2) < 1)height = 1;
MagickResizeImage(m_wand,width,height,LanczosFilter,1);
MagickSetImageCompressionQuality(m_wand,95);
MagickWriteImage(m_wand,"logo_resize.jpg");
if(m_wand)m_wand = (struct MagickWand *) DestroyMagickWand(m_wand);
MagickWandTerminus();
}
Only MagickWandGenesis() and MagickWandTerminus() are resolved.
The installation of the library ends correctly. How to solve?
edit:
Running I get the error:
/usr/local/include/ImageMagick-7/MagickWand/MagickWand.h:29:40: fatal error: MagickCore/magick-config.h: File o directory non esistente
# include "MagickCore/magick-config.h"
^
compilation terminated.
make[3]: *** [CMakeFiles/main.dir/main.c.o] Errore 1
make[2]: *** [CMakeFiles/main.dir/all] Errore 2
make[1]: *** [CMakeFiles/main.dir/rule] Errore 2
make: *** [main] Errore 2
I tried the solution shown here but does not work:
ImageMagick No such file or directory
The library structure is that:
ImageMagick-7
├── MagickCore
│ ├── magick-config.h
|
└── MagickWand
├── MagickWand.h
MagickWand.h includes some header in MagickCore.
I fix replacing include_directories(${ImageMagick_INCLUDE_DIRS})
with include_directories(/usr/local/include/ImageMagick-7).
I don't know why but if i print ${ImageMagick_INCLUDE_DIRS} this is not set.
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;
}
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
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})
I'm using Ubuntu 12 64 bit, installed fftw library version 2.1.5.
I have a c++ project with use CMake to build the make file. This is my cmakelist.text:
project(MP)
cmake_minimum_required(VERSION 2.8)
if(TYPE STREQUAL "Debug")
set(CMAKE_BUILD_TYPE "Debug")
else()
set(CMAKE_BUILD_TYPE "Release")
endif()
if(CMAKE_COMPILER_IS_GNUCXX)
add_definitions( -std=c++11 )
endif()
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_library(GLUI libglui.a ./vendor/lib)
include_directories(${OPENGL_INCLUDE_DIR}
./vendor/include)
LINK_DIRECTORIES(/usr/local/lib)
LINK_DIRECTORIES(/usr/lib)
LINK_DIRECTORIES(/usr/bin)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI})
When I tried running the make file create by Cmake, i got this problem:
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::init_simulation(unsigned long)':
Simulation.cc:(.text+0x2d5): undefined reference to `rfftw2d_create_plan'
Simulation.cc:(.text+0x2ee): undefined reference to `rfftw2d_create_plan'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::solve()':
Simulation.cc:(.text+0x881): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0x891): undefined reference to `rfftwnd_one_real_to_complex'
Simulation.cc:(.text+0xa7f): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0xa8f): undefined reference to `rfftwnd_one_complex_to_real'
CMakeFiles/SciVis.dir/Simulation.cc.o: In function `Simulation::FFT(int, void*)':
Simulation.cc:(.text+0x390): undefined reference to `rfftwnd_one_complex_to_real'
Simulation.cc:(.text+0x3a0): undefined reference to `rfftwnd_one_real_to_complex'
collect2: error: ld returned 1 exit status
make[2]: *** [SciVis] Error 1
make[1]: *** [CMakeFiles/SciVis.dir/all] Error 2
make: *** [all] Error 2
In my Simulation.cc file:
#include <fftw.h>
void Simulation::init_simulation(size_t n)
{
//Allocate data structures
size_t dim = n * 2 * (n / 2 + 1);
vx = new fftw_real[dim];
vy = new fftw_real[dim];
vx0 = new fftw_real[dim];
vy0 = new fftw_real[dim];
fx = new fftw_real[n * n];
fy = new fftw_real[n * n];
rho = new fftw_real[n * n];
rho0 = new fftw_real[n * n];
plan_rc = rfftw2d_create_plan(n, n, FFTW_REAL_TO_COMPLEX, FFTW_IN_PLACE);
plan_cr = rfftw2d_create_plan(n, n, FFTW_COMPLEX_TO_REAL, FFTW_IN_PLACE);
// Initialize data structures to 0
for (size_t i = 0; i < n * n; i++)
{
vx[i] = vy[i] = vx0[i] = vy0[i] = fx[i] = fy[i] = rho[i] = rho0[i] = 0.0f;
}
}
void Simulation::FFT(int direction,void* vx)
{
if(direction==1) rfftwnd_one_real_to_complex(plan_rc,(fftw_real*)vx,(fftw_complex*)vx);
else rfftwnd_one_complex_to_real(plan_cr,(fftw_complex*)vx,(fftw_real*)vx);
}
I dont know where I were wrong, can someone please help me ?
Thank you very much.
You're not linking against FFTW, you need to make CMake find the library and link against it first, put this file in your project's directory under a new folder "CMakeModules".
FindFFTW.cmake
# - Find FFTW
# Find the native FFTW includes and library
#
# FFTW_INCLUDES - where to find fftw3.h
# FFTW_LIBRARIES - List of libraries when using FFTW.
# FFTW_FOUND - True if FFTW found.
if (FFTW_INCLUDES)
# Already in cache, be silent
set (FFTW_FIND_QUIETLY TRUE)
endif (FFTW_INCLUDES)
find_path (FFTW_INCLUDES fftw3.h)
find_library (FFTW_LIBRARIES NAMES fftw3)
# handle the QUIETLY and REQUIRED arguments and set FFTW_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES)
mark_as_advanced (FFTW_LIBRARIES FFTW_INCLUDES)
Next, add this line to the top of your CMakeLists.txt:
project(MP)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})
Now try this:
find_package(FFTW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${FFTW_INCLUDES}
./vendor/include)
...
target_link_libraries(${PROJECT_NAME} GL GLU glut ${GLUI} ${FFTW_LIBRARIES})