glewInit() "Missing GL Version" in CMake Release builds only - opengl

I am building my OpenGL project using CMake with MSVC. It uses GLEW with GLFW. It runs fine when I build with --config Debug, but glewInit() fails when I run the build command with --config Release. I've tried both 64 and 32 bit builds, and it runs in debug but not release in both cases.
I've used add_definitions(-DGLEW_STATIC) in my CMakeLists.txt file to avoid needing #define GLEW_STATIC everywhere, but I've tried both and it doesn't make a difference.
I can see that the release glew.lib is getting produced (and glewd.lib gets produced for debug builds), and if I inspect the project properties in VS, the correct one is in the list of linker inputs.
I've tried with and without the window hints before creating a GL context.
There's something I don't understand going on here. Why is my GL version missing, only for Release build configuration?
EDIT: Adding minimum reproducible example (thanks squareskittles for the suggestion).
This is my CMakeLists.exe file:
cmake_minimum_required(VERSION 3.14)
project(Demo LANGUAGES CXX)
# Make sure GLEW gets built as a static lib
add_definitions(-DGLEW_STATIC)
option( glew-cmake_BUILD_SHARED "Build the shared glew library" OFF )
option( glew-cmake_BUILD_STATIC "Build the static glew library" ON )
# Build dependencies
add_subdirectory(glew)
add_subdirectory(glfw)
add_executable(demo demo/main.cpp)
target_compile_features(demo PUBLIC cxx_std_17)
target_compile_definitions(demo PRIVATE -D_SCL_SECURE_NO_WARNINGS)
target_include_directories(demo PUBLIC glew/include)
target_include_directories(demo PUBLIC glfw/include)
# Link dependencies
target_link_libraries(demo libglew_static glfw ${GLFW_LIBRARIES})
It might be important to know that glew-cmake is what is in my glew directory.
And demo/main.cpp:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cassert>
int main(int argc, char** argv)
{
// Open a window and initialize Glew
assert(glfwInit());
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(600, 400, "window", 0, 0);
glfwMakeContextCurrent(window);
std::cout << "GLFW: " << glfwGetVersionString() << "\n";
std::cout << "GLEW: " << glewGetString(GLEW_VERSION) << "\n";
GLenum glewStatus = glewInit();
if (glewStatus != GLEW_OK) {
std::cout << "GLEW ERROR: " << glewGetErrorString(glewStatus) << "\n";
exit(1);
}
std::cout << "GL: " << glGetString(GL_VERSION) << "\n";
// Shut down graphics
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
My build commands are run from a directory just below the level of my CMakeLists.txt file. My debug build command is:
cmake ..
cmake --build . --config Debug
If I build & run the debug executable, a window opens before terminating, and the output is:
GLFW: 3.3.0 Win32 WGL EGL VisualC
GLEW: 2.2.0
GL: 3.2.0 - Build 25.20.100.6617
And my release build command is:
cmake ..
cmake --build . --config Release
If I build & run the release executable, I hit the exit(1) line, and the output is:
GLFW: 3.3.0 Win32 WGL EGL VisualC
GLEW: 2.2.0
GLEW ERROR: Missing GL version
UPDATE: I've tried linking with shared libs (dlls) instead of static libs, and was confirmed that the bug still reproduces.
UPDATE: I tried building a VS project from scratch with only my minimum reproducible case, manually specifying either the static or dynamic linkages, and it actually works! So it seems like it must be something with my CMake configuration.

I found that I could get release builds to run with the exact same linkages if I manually set up a project in VS. I diffed the vcxproj file that I manually created with the one generated by CMake to figure out the differences in release configurations. It came down to the NDEBUG preprocessor definition.
I don't understand why this definition causes OpenGL not to be found (I suppose that's another question, but if you can answer it here I'll award you the answer). If I add the line
string( REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
to my CMakeLists.txt file, I'm able to build and run in release mode!

Related

How to control a robot from Gazebo using ROS2 Foxy?

I'm utterly confused by the Gazebo/Ignition simulation programs. I'm currently using ROS2 Foxy with Ubuntu 20.04 and looking forward to controlling an UAV modeled in Gazebo via ROS2. However I'm completely lost even though there's "documentation" (which I've found it to be really mixed up between all the different versions of ROS).
What I've done so far is trying to run this script:
#include <iostream>
#include <ignition/msgs.hh>
int main()
{
ignition::msgs::Vector3d point1;
point1.set_x(1);
point1.set_y(3);
point1.set_z(5);
ignition::msgs::Vector3d point2;
point2.set_x(2);
point2.set_y(4);
point2.set_z(6);
std::cout << "Point1:\n" << point1.DebugString() << std::endl;
std::cout << "Point2:\n" << point2.DebugString() << std::endl;
return 0;
}
However I get the message that the build fails since there is no ignition directory even though I have installed it.
I'd appreciate any help in this matter since I feel I'm completely lost with installations and the CMakeLists.txt file.
Gustavo,
with this CMakeLists.txt you can compile your code:
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
find_package(gz-cmake3 REQUIRED)
project(message-example)
gz_find_package(gz-msgs9 REQUIRED)
set(GZ_MSGS_VER ${gz-msgs9_VERSION_MAJOR})
add_executable(message-example main.cpp)
set_property(TARGET message-example PROPERTY CXX_STANDARD 17)
target_link_libraries(message-example
PRIVATE gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER})
then execute
mkdir build
cd build
cmake ..
./message-example
I don't know which Gazebo version are you using but the version for gz-msgs might be different:
Garden - msgs9
Fortress - msgs8
Citadel - msgs5
You can find here some examples related with Gazebo or if you are going to use Gazebo and ROS you should review this other repository ros_gz.

Error when trying to link GLFW from source to project on c++ via CLion on Ubuntu [duplicate]

This question already has an answer here:
Undefined Reference error using OpenGL in CLion
(1 answer)
Closed 2 years ago.
I'm trying to add GLFW from source to my project. I did as stated in the documents, but I get an error.
Error:
/usr/bin/ld: CMakeFiles/mancala_graphics.dir/main.cpp.o: in function `main':
/path/to/clion/mancala_graphics/main.cpp:47: undefined reference to 'glClear'
collect2: error: ld returned 1 exit status
make[ 3 ]: *** [CMakeFiles/mancala_graphics.dir/build.make:88: mancala_graphics] Error 1
make[ 1 ]: *** [CMakeFiles/Makefile2:115: CMakeFiles/mancala_graphics.dir/all] Error 2
make[ 2 ]: *** [CMakeFiles/Makefile2:122: CMakeFiles/mancala_graphics.dir/rule] Error 2
make: *** [Makefile:164: mancala_graphics] Error 2
Source Code is below and its from documentation:
#include <GLFW/glfw3.h>
int main() {
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
CMake is below and this is from documentation also:
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics glfw)
Below image shows file hierarchy of the project:
It seems like glClear defined in gl.h as you see in picture
When I go to gl.h it is not in source files which I downloaded and save Dependencies/ but in /usr/include/GL/gl.h it may be the source of error, because when I open the header file I saw a warning says the file doesn't belong to any project as you see:
Question is: What is wrong with this configuration and why I can't run the code snippet from documentation?
Also question is: How can I add GLFW from source to my project?
IDE: CLion
OS: Linux / Ubuntu
EDIT
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
find_package(OpenGL REQUIRED)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics OpenGL::GL)
target_link_libraries(mancala_graphics glfw)
When I write cmake like above code working but it feels something wrong.
From the GLFW documentation:
Note that the glfw target does not depend on OpenGL, as GLFW loads any OpenGL, OpenGL ES or Vulkan libraries it needs at runtime. If your application calls OpenGL directly, instead of using a modern extension loader library, use the OpenGL CMake package.
find_package(OpenGL REQUIRED)
target_link_libraries(myapp OpenGL::GL)
If OpenGL is found, the OpenGL::GL target is added to your project, containing library and include directory paths. Link against this like above.

SDL_Mixer 2.0.4 "MP3 support not available" even though libmpg123 is installed

I am a beginner to programming and am trying to make a simple console MP3 player as a project. For this, I need a way to play .mp3 files. SDL Mixer provides this facility but requires libmpg123 installed on one's system.
Call to 'Mix_Init()' always fails even though I have the required dependency 'libmpg123' installed. I do not have the dependencies for other formats i.e., FLAC, OGG, MOD and MIDI if that matters.
I am running Ubuntu 18.04 on my laptop and I have SDL2 version 2.0.9, SDL_Mixer 2.0.4 both built from source (although I have tried the versions available through the apt package manager). I also have libmpg123 installed which I have also built from source (again, I have also tried the version available through the package manager).
I compiled the code in two ways:
g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer
and
g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer -lmpg123
I don't know which one is right but neither one fixes the problem.
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<iostream>
int main(int argc, char** argv)
{
if(SDL_Init(SDL_INIT_AUDIO))
std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
else {
int result {0};
if(!((result = Mix_Init(MIX_INIT_MP3)) && MIX_INIT_MP3)){
std::cerr << Mix_GetError() << std::endl;
std::cerr << "Mix_Init() returns " << result << std::endl;
}
else {
std::cout << "Success!" << std::endl;
}
}
return 0;
}
Here are the contents of my /usr/local/lib directory:
cmake libmpg123.so.0.44.8 libSDL2-2.0.so.0 libSDL2main.la libSDL2_mixer.so pkgconfig
libglfw3.a libout123.la libSDL2-2.0.so.0.9.0 libSDL2_mixer-2.0.so.0 libSDL2.so python2.7
libmpg123.la libout123.so libSDL2.a libSDL2_mixer-2.0.so.0.2.2 libSDL2_test.a python3.6
libmpg123.so libout123.so.0 libSDL2.la libSDL2_mixer.a libSDL2_test.la python3.7
libmpg123.so.0 libout123.so.0.2.2 libSDL2main.a libSDL2_mixer.la mpg123
I expect the output:
Success!
Instead I get:
MP3 support not available
Mix_Init() returns 0
UPDATE:
Apparently, I have the other dependencies installed as well...
But I have no idea how to use them.
You need to have development files for mpg123 installed when you're building SDL2_mixer. E.g. for debian/ubuntu you'll need libmpg123-dev (and likewise for other formats), and SDL_mixer's ./configure should output something like
checking mpg123.h usability... yes
checking mpg123.h presence... yes
checking for mpg123.h... yes
checking for mpg123_replace_reader_handle in -lmpg123... yes
-- dynamic libmpg123 -> libmpg123.so.0
Then make && make install. Or use distro-provided libsdl2-mixer-dev, if it fits your requirements.

OpenCV doesn't work with Visual Studio

I just installed openCV ver. 3.00 beta on my computer and I'm trying to test it with VS2013. I followed quick start guide:
1) Installation for windows, using pre-build libraries.
2) OpenCV inside Microsoft VS, using local method and x64/vc12 directory for libraries.
As a result, their example(listed in link #2) produced an error:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window", image); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
The output is:
1>------ Build started: Project: test_openCV, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1104: cannot open file 'opencv_calib3d300d.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
As far as I understand, my program can see openCV libraries, but for some reason cannot open them. Please help.
Thanks
Update: Tried to do what was recommended in first four comments, no result still. So I will post here full information:
Project: CUDA 6.5 (I need to work with CUDA).
Source file: Source.cpp
Mode: Debug, x64.
Enviromental variables:
OPENCV_DIR: C:\programs\misha\opencv\build\x64\vc12
Path (the last string): %OPENCV_DIR%\bin
Page Properties:
C\C++ -> General -> Additional Include Directories(last string):
$(OPENCV_DIR)\..\..\include
Linker -> General -> Additional Library Directories(last string):
$(OPENCV_DIR)\lib
Linker -> Input -> Additional Dependencies:
opencv_calib3d300d.lib
opencv_core300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_shape300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_ts300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib
Output:
1>------ Build started: Project: test_openCV, Configuration: Debug x64 ------
1>LINK : fatal error LNK1104: cannot open file 'opencv_calib3d300d.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So, I cannot build x64 project for some reason. Error is the same.
Update 2: If you can build this example (or any example with OpenCV), can you please send it on genkin#physics.msu.ru? I will figure it out by myself
Unfortunately that solution did not work for me.
I made it work by adding as additional dependencies(Linker -> Input -> Additional Dependencies:) only those libraries located in my $(OPENCV_DIR)\lib folder. Namely: opencv_ts300d.lib;opencv_world300d.lib.
After a long time of different attempts, I found the answer here. For x64 project, in addition to what I did above, you have to:
1) Restart your PC after setting enviromental variables. Variable Path was not updated on my computer without restart.
2) In addition to libraries added above, you have to add (although, for this example some of them might not be used):
ippicvmt.lib
IlmImfd.lib
libtiffd.lib
libpngd.lib
libjasperd.lib
libjpegd.lib
libwebpd.lib
zlibd.lib
comctl32.lib
Edit: I recently had to install it again on a new PC. As user1060873 pointed out, you have to add only the libraries located in your $OPENCV$/lib folder. For example, on my PC it is C:\opencv\build\x64\vc12\lib. Note, that for debug mode you should add opencv_world310d.lib, and for release opencv_world310.lib
I'm also facing the same problem. I am using win7 x64 with VS2015 and OpenCV 3.2
After several hours of struggling I finally found out the answer. Each version is not the same, and there may be some differences. So this is what I did :
Find where your libraries are for Additional Library, it is located on $OPENCV$/lib folder
ONLY put those is $OPENCV$/lib folder into the Additional Library (Project properties, Configuration Properties, Linker, Input). In my case I put opencv_world300d.lib (according to official tutorial which is not up to date!) into my Additional Library. But after I check the library folder, there is opencv_world320d.lib, so instead of 300 I write 320.
Solved this problem by unchecking the "inherit from parent or project defaults" checkbox under Linker->Input->Additional Dependencies
I added "opencv_world300d.lib" since Im using opencv 3.0.0 build x64 vc11

How to get std::cout to show in eclipse IDE console when using boost.test?

As the question says, I am having trouble seeing std::out statements in the eclipse console when using the "c/c++ unit" run configuration with boost.test.
Here is a simple example describing the issue.
quickEclipse_stdout.cpp:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "I see this in the eclipse console!\n";
std::cout << "I also see this in the terminal!\n";
return(0);
}
quickEclipse_stdout_test.cpp:
#include <iostream>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(quickTest_stdout)
BOOST_AUTO_TEST_CASE(bla)
{
std::cout << "I cannot see this in the eclipse console" << std::endl;
std::cout << "but I can see it when run outside eclipse in terminal" << std::endl;
BOOST_CHECK_MESSAGE(true, "of course I see this in eclipse c/c++ test log");
}
BOOST_AUTO_TEST_SUITE_END()
I used cmake to generate project files for eclipse.
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(quickTest CXX)
FIND_PACKAGE(Boost COMPONENTS program_options filesystem regex unit_test_framework)
IF (Boost_FOUND)
MESSAGE("Boost found!!!")
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
#main output to console works
ADD_EXECUTABLE(quickEclipse_stdout, quickEclipse_stdout.cpp)
#boost.test output to console doesnt work
ADD_EXECUTABLE(quickEclipse_stdout_test UTDriver.cpp quickEclipse_stdout_test.cpp)
TARGET_LINK_LIBRARIES(quickEclipse_stdout_test ${Boost_LIBRARIES})
UTDriver.cpp:
#define BOOST_TEST_MODULE myQuickEclipseUT
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
To generate the eclipse project I run the following command:
cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_ECLIPSE_VERSION=4.3 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_ARG1=-std=c++11 ~/mySrcDir
I open the project in eclipse and enter c++ perspective
I then build the executables by double-clicking on make targets (quickEclipse_stdout, quickEclipse_stdout_test)
I goto run configurations and create "c/c++ application" for quickEclipse_stdout.
I goto run configurations and create "c/c++ unit" for quickEclipse-stdout_test and select boost as the test runner.
I run quickEclipse_stdout and see the output in eclipse console.
I run quickEclipse_stdout_test and the eclipse console is empty. I see the message in the c/c++ unit messages tab.
I can see the std::out on both if I run the executables in a terminal outside eclipse.
How can I get the std::out to show in the eclipse console when running the quickEclipse_stdout_test?
I am on Linux CentOS 6.5.
Eclipse Standard/SDK
Version: Kepler Service Release 1
Build id: 20130919-0819
Eclipse C/C++ Development Tools
Version: 8.2.1.201309180223
Im sure based on your description its redirecting stdout to its own window for testing purposes
I am exactly in your situation and I think it is a missing feature of the C/C++ unit test plugin in Eclipse.
The plugin requires the whole test output in xml format (--output_format=XML) which puts the stdout messages in the node of the xml output. The output will be directly parsed by the plugin and the information is organized in the according view. The plugin misses the option to show the stdout messages. I could simply have a further icon beside error, warning, information to show the stdout messages.
I have opened an issue for this on github: https://github.com/xgsa/cdt-tests-runner/issues/14 but the project it not actively maintained the last years...