Trouble getting QPrinter to link using cmake - c++

So I've been trying to get my programs with QPrinter to work compile with cmake+mingw+qt5.2 but I'm having issues: the following test program doesn't compile because it cant find QPrinter which should be part of QtCore
#include <QPrinter>
#include <QApplication>
#include <windows.h>
int main()
{
QApplication a( argc, argv );
return 0;
} // end
this is my cmake file
SET(CMAKE_C_COMPILER E:/Qt/Qt5.2.1/Tools/mingw48_32/bin/gcc.exe)
SET(CMAKE_CXX_COMPILER E:/Qt/Qt5.2.1/Tools/mingw48_32/bin/g++.exe)
cmake_minimum_required(VERSION 2.8)
PROJECT (test_prog)
add_definitions(-std=c++11)
SET( test_prog_SRCS test.cpp)
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Widgets finds its own dependencies.
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
include_directories(
${Qt5Widgets_INCLUDE_DIRS}
${Qt5Gui_INCLUDE_DIRS}
${Qt5Core_INCLUDE_DIRS}
)
add_executable(test_prog WIN32 ${test_prog_SRCS})
target_link_libraries(test_prog ${Qt5Widgets_LIBRARIES} ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} )
The Error is:
test.cpp:1:20: fatal error: QPrinter: No such file or directory
#include <QPrinter>
Does anyone know the right incantations to get this to work?

With CMake 2.8.11:
SET(CMAKE_C_COMPILER E:/Qt/Qt5.2.1/Tools/mingw48_32/bin/gcc.exe)
SET(CMAKE_CXX_COMPILER E:/Qt/Qt5.2.1/Tools/mingw48_32/bin/g++.exe)
cmake_minimum_required(VERSION 2.8.11)
PROJECT (test_prog)
add_definitions(-std=c++11)
SET( test_prog_SRCS test.cpp)
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5PrintSupport REQUIRED)
add_executable(test_prog WIN32 ${test_prog_SRCS})
target_link_libraries(test_prog Qt5::PrintSupport)

Related

C++ Linking a custom library built from source (CMakeLists.txt)

I built Open3D from source into a /home/user/custom_location/Open3D/build
I have a project with a dependency on Open3D and a CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.12)
project(graph_filter)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
# Pybind11
find_package(pybind11 REQUIRED)
# OpenMP
find_package(OpenMP REQUIRED)
# Eigen
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
# Open3D
list(APPEND CMAKE_INSTALL_PREFIX "~/libraries/")
find_package(Open3D HINTS ${CMAKE_INSTALL_PREFIX}/lib/CMake)
list(APPEND Open3D_LIBRARIES dl)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Open3D_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${Open3D_EXE_LINKER_FLAGS}")
include_directories(${Open3D_INCLUDE_DIRS})
link_directories(${Open3D_LIBRARY_DIRS})
pybind11_add_module(
graph_filter
wrapper.cpp
)
target_link_libraries(graph_filter PRIVATE ${Open3D_LIBRARIES} Eigen3::Eigen OpenMP::OpenMP_CXX)
How can I link it? When I run
INSTALL_DIR=/home/user/custom_location/Open3D/build
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}```
I am getting:
Could NOT find Open3D (missing: Open3D_DIR)
fatal error: Open3D/Open3D.h: No such file or directory
2 | #include <Open3D/Open3D.h>
What is more confusing is the contents of Open3D/build folder after building from source.
I just want to resolve this dependency on Open3D. Thanks for any help!!!

Qt5 cmake Windows Linker Error

I just wanted to start a new project which uses Qt in combination with CMake. I (hope I) followed the docs, but it seems as if the linker is not doing its work correctly:
CMakeFiles\QtTest.dir/objects.a(main.cpp.obj): In function `QString::QString(char const*)':
P:/Qt/5.9.1/msvc2015_64/include/QtCore/qstring.h:659: undefined reference to `__imp__ZN7QString16fromAscii_helperEPKci'
CMakeFiles\QtTest.dir/objects.a(main.cpp.obj): In function `QTypedArrayData<unsigned short>::deallocate(QArrayData*)':
P:/Qt/5.9.1/msvc2015_64/include/QtCore/qarraydata.h:237: undefined reference to `__imp__ZN10QArrayData10deallocateEPS_yy'
I'm using mingw-w64 (the x86_64 installation) with CLion and its bundled CMake and Qt 5.9.1. My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.8)
project(QtTest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
set(CMAKE_VERBOSE_MAKEFILE ON)
# Qt
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(Qt5_NO_LINK_QTMAIN ON)
set(CMAKE_PREFIX_PATH P:/Qt/5.9.1/msvc2015_64/lib/cmake)
find_package(Qt5Core REQUIRED)
get_target_property(QtCore_location Qt5::Core LOCATION)
message("QtCore version ${Qt5Core_VERSION} is at ${QtCore_location}")
# QtTest executable
set(SOURCE_FILES main.cpp)
add_executable(QtTest ${SOURCE_FILES})
target_link_libraries(QtTest Qt5::Core)
When creating the build files it outputs (correctly) QtCore version 5.9.1 is at P:/Qt/5.9.1/msvc2015_64/bin/Qt5Core.dll
The main.cpp contains just a call to QString for testing purposes:
#include <QtCore/QString>
int main() {
QString string = QString("test");
}
Why does this happen?

CMake cannot find QXmlSimpleReader

I have a C++ header file which has the following lines:
#include <QXmlSimpleReader>
#include <QXmlDefaultHandler>
and my cmake has the following lines:
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Xml REQUIRED)
When running CMake I get the following error message:
QXmlSimpleReader: No such file or directory
#include <QXmlSimpleReader>
What am I doing wrong?
For some reason it do not adds to project include dirs.
Add this one to your cmake
INCLUDE_DIRECTORIES( ${Qt5Xml_INCLUDE_DIRS} )
I guess you forgot to link against Qt5xml. A working example from the documentation for cmake 2.8.11 and later, modified to link against Qt5Xml:
cmake_minimum_required(VERSION 2.8.11)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Xml)
# Tell CMake to create the helloworld executable
add_executable(helloworld WIN32 main.cpp)
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Xml)

library can't see its own headers

FindFoo.cmake:
# Foo_FOUND - system has Foo
# Foo_INCLUDE_DIRS - the Foo include directories
# Foo_LIBRARIES - link these to use Foo
# Foo_VERSION
# Foo_DEFINITIONS - compiler switches required for using Foo
find_package(PkgConfig)
pkg_check_modules(PC_Foo QUIET Foo)
find_path(Foo_INCLUDE_DIR Foo/Foo.h
PATHS ${PC_Foo_INCLUDEDIR} ${PC_Foo_INCLUDE_DIRS}
)
find_library(Foo_LIBRARY Foo
PATHS ${PC_Foo_LIBDIR} ${PC_Foo_LIBRARY_DIRS}
)
set(Foo_VERSION ${PC_Foo_VERSION})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Foo
FOUND_VAR Foo_FOUND
REQUIRED_VARS
Foo_LIBRARY
Foo_INCLUDE_DIR
VERSION_VAR Foo_VERSION
)
if(Foo_FOUND)
set(Foo_LIBRARIES ${Foo_LIBRARY})
set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
endif()
mark_as_advanced(
Foo_INCLUDE_DIR
Foo_LIBRARY
)
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
PROJECT(sandbox)
set(CMAKE_AUTOMOC ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
FIND_PACKAGE(Qt5Core REQUIRED)
find_package(Foo REQUIRED)
include_directories(${Foo_INCLUDE_DIRS})
SET(sandbox_SOURCES main.cpp)
ADD_EXECUTABLE(sandbox ${sandbox_SOURCES})
QT5_USE_MODULES(sandbox Core)
TARGET_LINK_LIBRARIES(sandbox ${Foo_LIBRARIES})
#include <Bar1.h> works in my program whereas #include <Foo/Bar1.h> doesn't. and the real problem is that i can't build my program because Bar1.h contains #include <Foo/Bar2.h> and:
...Foo/Bar1.h: fatal error: Foo/Bar2.h: No such file or directory
compilation terminated.
CMake 3.5.1
looks like the problem was CMake cache. i deleted everything from the CMakeFiles directory and then CMake couldn't configure the project:
Could NOT find Foo (missing: Foo_INCLUDE_DIR)
i had to add PATH_SUFFIXES to find_path(), and all the problems are gone.

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)