Getting reference issue in Qt while implementing OpenSceneGraph - c++

I am getting error (**
**> CMakeFiles\untitled3.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x46):
undefined reference to `_imp___ZN9osgViewer6ViewerC1Ev'**)
while adding OpenSceneGraph in Qt. My profile is cmakelist.txt.
cmake_minimum_required(VERSION 2.8.12)
project(untitled3)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
FIND_PATH(OPENSCENEGRAPH_INCLUDE_DIR osg/Referenced
PATHS
$ENV{OSG_ROOT}/include
$ENV{OSG_DIR}/include
/usr/include
/usr/local/include
)
FIND_PATH(OPENSCENEGRAPH_LIB_DIR libosg.so osg.lib
PATHS
$ENV{OSG_ROOT}/lib
$ENV{OSG_DIR}/lib
/usr/lib
/usr/local/lib
)
INCLUDE_DIRECTORIES(common ${OPENSCENEGRAPH_INCLUDE_DIR})
LINK_DIRECTORIES(${OPENSCENEGRAPH_LIB_DIR})
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} Qt5::Core)
My main.cpp file is
#include <QCoreApplication>
#include <osgViewer/Viewer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
osgViewer::Viewer viewer;
// viewer.setSceneData( osgDB::readNodeFile("cessna.osg") );
// return viewer.run();
return a.exec();
}
Any folks who can help. I invite them to come forward.

There is a problem with your cmake file. You need to make sure to:
Find the necessary OSG packages (osgViewer in your case).
Link your executable to the target OSG libraries.
Just like you did it for QtCore:
find_package(Qt5Core)
find_package(OpenSceneGraph REQUIRED COMPONENTS osgViewer)
# ...
target_link_libraries(${PROJECT_NAME} Qt5::Core ${OPENSCENEGRAPH_LIBRARIES} )

Related

QOpenGLWidget does not show on wayland: Protocol error

I was trying to make some tests with the QOpenGLWidget class (Qt6), and I wrote the following main.cpp:
#include <QApplication>
#include <QOpenGLWidget>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QOpenGLWidget win(nullptr);
win.show();
return app.exec();
}
But, when executing on ubuntu/wayland, I get the following error:
The Wayland connection experienced a fatal error: Protocol error
If I set the following environment variable, works fine (a black window is shown):
QT_QPA_PLATFORM=xcb
Do I need to set some additional settings to have it working on wayland natively?
Or is there something wrong with my system configuration?
I am building with cmake:
cmake_minimum_required(VERSION 3.13)
project(FirstQtWindow)
find_package(OpenGL REQUIRED)
find_package(Qt6 COMPONENTS Gui OpenGLWidgets)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
add_executable(first-qt-window
main.cpp)
target_link_libraries(
first-qt-window
PUBLIC
OpenGL::GLU
Qt6::Core
Qt6::OpenGLWidgets)

Trouble with setting up SDL2_Image in CLion: undefined reference to `IMG_Init'

I've been trying to setup the extension library SDL2_image for SDL2 on CLion (MingW). Steps for installation I've taken so far:
Copy the contents of SDL2_image development into C:\MingW
Created a FindSDL2_IMAGE.cmake file and linked it.
Here's my CMakeList.txt file:
cmake_minimum_required(VERSION 3.19)
project(TestGame)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
find_package(SDL2_IMAGE REQUIRED)
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
add_executable(TestGame src/main.cpp src/GameWindow.cpp src/GameWindow.h)
target_link_libraries(TestGame ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
And the relevant main.cpp file:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main(int argc, char *args[]) {
IMG_Init(IMG_INIT_PNG);
}
Edit: I've forgot to include the error message.
CMakeFiles\TestGame.dir/objects.a(main.cpp.obj): In function `Z4initv':
D:/_dev/C++/TestGame/src/main.cpp:14: undefined reference to `IMG_Init'

cLion + Qt5 - exit code -1073741515 (0xC0000135)

I'm trying to run simple test using QT5 and cLion but I run in to the exit code wall... Here is my envi :
cLion 2017.2
minGw 5.0 < according to cLion
cMake 3.8.2
Qt 5.9.0
CMakeList.txt
cmake_minimum_required(VERSION 3.8)
project(testWindotQt)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp )
add_executable(testWindotQt ${SOURCE_FILES})
if (WIN32)
# If you compile on windows replace path to your Qt folder
set(CMAKE_PREFIX_PATH "C:\\Qt\\5.9\\mingw53_32")#\\lib\\cmake")
endif()
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
qt5_use_modules(testWindotQt Core Widgets Gui)
target_link_libraries(testWindotQt Qt5::Widgets Qt5::Core Qt5::Gui)
main.cpp
#include <iostream>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
int main(int argc, char *argv[])
{
std::cout << "Hello, World!" << std::endl;
QApplication a(argc, argv);
QLabel *label = new QLabel("HeyYou");
label->show();
return a.exec();
}
Execution >
Process finished with exit code -1073741515 (0xC0000135)
Can any1 help me out with this error please?
Regards
Dariusz
Have a look at this post: Setting up Qt for CLion
Basically, you have to add C:\QT\5.x\mingwxx_32\bin to your PATH environment variable.

cmake does not consider -pthread

I am trying to make a testbench to my program using gmock/gtest; Linux/Ubuntu; KDevelop/CMake. From the link error message I conclude that part of the gtest package is missing pthread support.
/home/projects/cpp/gmock/gtest/libgtest.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x16): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x2b): undefined reference to `pthread_key_delete'
I also read
googletest: how to setup?
Using g++ directly, everything works. So, since I am usinc KDevelop/CMake, I suspect either my code or CMake.
In my CMakeLists.txt I do use
add_definitions( -pthread -m64)
However, I do not see any effect in the Makefile.
Am I missing something from my CMakeLists.txt, or CMake does not consider the line above?
My CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_definitions( -Dpthread )
project(ThreadTest)
INCLUDE_DIRECTORIES(gmock/gtest/include)
set ( GTEST_LIBS libgtest.a )
link_directories( ~/projects/cpp/gmock/gtest)
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_LIBS})
Do I misunderstand, that add_definitions should work in this situation?
After reading
How do I force cmake to include "-pthread" option during compilation?
my question really looks like duplicate. However,
cmake_minimum_required(VERSION 2.8)
add_definitions( -Dpthread )
project(ThreadTest)
INCLUDE_DIRECTORIES(gmock/gtest/include)
find_package( Threads )
set ( GTEST_LIBS libgtest.a )
link_directories( ~/projects/cpp/gmock/gtest)
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})
still gives the warning 'Could NOT find Threads'. I tried to search Ubuntu software center for "threads", with no result. After that, I installed libghc-threads-dev. However, when using
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
I keep receiving 'Could NOT find Threads', as error. What shall I do to satisfy find_package, and why do I have this problem, when the simple Makefile produces what I expect?
PS: my main file:
#include "gmock/gtest/include/gtest/gtest.h"
TEST(blahTest, blah1) {
EXPECT_EQ(1, 1);
}
int main (int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
int returnValue;
returnValue = RUN_ALL_TESTS();
return returnValue;
}
After busy hours, I succeeded to compile my supercomplex test program, using KDevelop/CMake/gtest
#include "gtest-1.7.0/include/gtest/gtest.h"
TEST(blahTest, blah1) {
EXPECT_EQ(1, 1);
}
int main (int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
The CMakeLists.txt file that could do the task is
# http://stackoverflow.com/questions/13513905/how-to-properly-setup-googletest-on-linux/13513907#13513907
# http://stackoverflow.com/questions/15193785/how-to-get-cmake-to-recognize-pthread-on-ubuntu
# http://stackoverflow.com/questions/21116622/undefined-reference-to-pthread-key-create-linker-error
# http://stackoverflow.com/questions/1620918/cmake-and-libpthread
# https://meekrosoft.wordpress.com/2009/10/04/testing-c-code-with-the-googletest-framework/
# http://stackoverflow.com/questions/30106608/googletest-cmake-and-make-tests-not-running
# http://stackoverflow.com/questions/13521618/c-project-organisation-with-gtest-cmake-and-doxygen
# http://www.kaizou.org/2014/11/gtest-cmake/
cmake_minimum_required(VERSION 2.8)
project(ThreadTest C CXX)
ADD_SUBDIRECTORY (gtest-1.7.0)
enable_testing()
#set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
if(NOT MSVC)
set(PThreadLib -pthread)
endif()
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${PThreadLib} ${GTEST_LIBRARIES})
#add_test(ThreadTest ThreadTest)
I want to highlight some things: a few valuable links and
project(ThreadTest C CXX) rather than project(ThreadTest)
and
set(PThreadLib -pthread) rather than set(PThreadLib pthread)
I guess the rest can be managed. It could be a good starting point.
I integrated Google Test framework in my own project and this CMake file is working.
# Google C++ Testing Framework
# https://code.google.com/p/googletest/
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
if(WIN32)
# GTest is static compiled by default
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()
enable_testing()
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_BOTH_LIBRARIES})
CMake warning "Could NOT find Threads" is related to a bug:
CMake failing to detect pthreads due to warnings
All you need to do is link with pthread:
target_link_libraries(BUILD_ARTIFACT pthread)
BUILD_ARTIFACT can be your project, or library name. You should add this line after defining your artifact.
For example, if your build artifact is ThreadTest:
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest pthread)

Preparing Qt for Windows

I'm trying to setup Qt inside windows 8.1 using jetbrains Clion IDE but it does not show anything after compiling a simple test project. This is my main.cpp file:
#include <QApplication>
#include <QtWidgets/qpushbutton.h>
int main(int argc, char **argv)
{
QApplication *app = new QApplication(argc, argv);
QPushButton *X = new QPushButton("test");
X->show();
return app->exec();
}
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(QtTest)
set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.5.0\\5.5\\mingw492_32\\")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCE_FILES main.cpp)
add_executable(QtTest WIN32 ${SOURCE_FILES})
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Multimedia REQUIRED)
qt5_use_modules(QtTest Core Widgets Gui Multimedia)
target_link_libraries(QtTest Qt5::Widgets Qt5::Gui Qt5::Core Qt5::Multimedia)
The output:
Process finished with exit code -1073741515 (0xC0000135)
Now I want some help to solve this problem.
The explicit memory management is unnecessary. You should also never need to use explicit Qt module prefixes - if you do, the build is not configured correctly. Finally, never use the qclass.h includes, use QClass.
Your code should read as follows. When your environment is correctly configured, it should build and run without any errors.
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QPushButton button("test");
button.show();
return app.exec();
}