Running application from Visual Studio causes errors (sol. generated by CMake) - c++

This is my first time asking a question, so please be chill if I do something wrong.
Hello,
My Visual Studio solution is generated by CMake.
Code is in C++ (and QML).
I'm creating a QtQuick window which uses .qml files to generate its content.
When I run the built application from Visual Studio (2017 RC) the app gives a common
"Component not ready" error message.
But if I run the same .exe file manually, everything is working perfectly.
I suspect it must be some kind of a mistake in my CMakeLists causing Visual Studio searching for files in the wrong place but I have no idea what that mistake could be.
C++ code fail part:
...
QQmlComponent guiComponent(qmlEngine,
QUrl::fromLocalFile(APP_RESOURCES"/qml/GUI.qml"));
guiRoot = qobject_cast<QQuickItem *>(guiComponent.create());
guiRoot->setParentItem(mainWindow->contentItem());
qDebug() << guiComponent.errors();
...
My CMakeLists:
cmake_minimum_required(VERSION 3.8)
set(PROJECT_NAME "[MyProjectName]")
set(APP_NAME "[MyProjectName]")
project(${PROJECT_NAME})
set(CMAKE_AUTORCC ON )
set(CMAKE_AUTOUIC ON )
set(CMAKE_AUTOMOC ON )
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules;${CMAKE_MODULE_PATH}")
find_package(Qt5 REQUIRED Quick QuickWidgets)
find_package(QtImageLoader REQUIRED HINTS ${CMAKE_SOURCE_DIR}/addons/QtImageLoader/cmake)
find_package(AssimpModelLoader REQUIRED HINTS ${CMAKE_SOURCE_DIR}/addons/AssimpModelLoader/cmake)
find_package(GPUEngine REQUIRED geGL geSG)
set(SRC_ROOT "${CMAKE_SOURCE_DIR}/src")
set(SRC_FILES "")
set(INCLUDE_DIRS "")
add_subdirectory(src)
list(APPEND SRC_FILES ${src_SRC_FILES})
list(APPEND INCLUDE_DIRS ${src_INCLUDE_DIRS})
message("Located source files:")
FOREACH(_FILE ${SRC_FILES})
message("- ${_FILE}")
ENDFOREACH()
message("Located include directories:")
FOREACH(_DIRE ${INCLUDE_DIRS})
message("- ${_DIRE}")
ENDFOREACH()
source_group(TREE ${SRC_ROOT} PREFIX "src" FILES ${SRC_FILES})
add_executable(${APP_NAME} ${SRC_FILES})
target_include_directories(${APP_NAME} PUBLIC ${INCLUDE_DIRS})
target_link_libraries(${APP_NAME} Qt5::Quick Qt5::QuickWidgets geGL geSG AssimpModelLoader QtImageLoader)
set_target_properties(${APP_NAME} PROPERTIES COMPILE_DEFINITIONS "APP_RESOURCES=\"Data\"")
set_property(TARGET ${APP_NAME} PROPERTY FOLDER "${APP_NAME}")
add_custom_command(TARGET ${APP_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/libs/$<CONFIG>"
"$<TARGET_FILE_DIR:[MyProjectName]>"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/resources"
"$<TARGET_FILE_DIR:[MyProjectName]>/Data"
)
Edit: I had this problem for a long time now (months) and didn't bother to fix it because running the application manually was sufficient. But now I want to use Visual Studio's debugging tools and I can't.

Adding
VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)"
to my target properties like this
set_target_properties(${APP_NAME} PROPERTIES COMPILE_DEFINITIONS "APP_RESOURCES=\"Data\""
VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)")
fixed the issue.
For additional solutions, look at vre's comments above.

Related

CMake 3.22.4 does not compatible with GTest 1.10.0.20201025-1.1

I've build my unit test code on Ubuntu 21.10, CMake 3.18.4 and GTest 1.10.0.20201025-1.1.
I wrote CMakeList.txt file as this.
# The minimum version of CMake Required
cmake_minimum_required (VERSION 2.8.12)
# Any project name will suffice, this has connotaions when using advanced CMake Features
set(PROJECT_NAME tests)
project (${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Just in case someone had the include files in seperate directory
include_directories(../include)
include_directories(..)
# This uses the complete student's library
aux_source_directory(.. SRC_LIST)
list(REMOVE_ITEM SRC_LIST "../main.cpp")
message ( STATUS "Compiling test_lib with following files ${SRC_LIST}" )
add_library(test_lib ${SRC_LIST})
# Now we make the gtests
set(GTEST_ROOT "/usr/src/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
if(NOT GTEST_LIBRARY)
message("GTest library not found")
endif()
add_executable(rawTests test_rawdata.cpp)
target_link_libraries(rawTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(rawTests test_lib)
add_executable(timeTests test_time.cpp)
target_link_libraries(timeTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(timeTests test_lib)
It works properly on my end.
But when I deliver this to my friend who uses CMake 3.22.4, it throws error look like this
Error Image
It's kind of weird issue and I didn't ever faced this sort of issue before.
I wonder anybody who has deep knowledge for CMake and GTest can help me to handle this.
Thank you in advance.

Encounter issue with `find_package` command in Visual Studio CMake

I'm operating under a new learning curve here with c++ and using CMake in Visual Studio. Here is the partial code up until the point where I receive the error:
project(libfranka
VERSION 0.8.0
LANGUAGES CXX
)
list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(MSVC)
add_compile_options(/W0)
else()
add_compile_options(-Wall -Wextra)
endif()
set(THIRDPARTY_SOURCES_DIR "${CMAKE_SOURCE_DIR}/3rdparty" CACHE PATH
"Directory for third-party sources")
## Dependencies
find_package(Poco REQUIRED COMPONENTS Net Foundation)
find_package(Eigen3 REQUIRED)
Once it hits the first find_package is where I encounter the error:
Here is the code within FindPoco.cmake.
find_package(Poco COMPONENTS ${Poco_FIND_COMPONENTS} CONFIG QUIET)
if(Poco_FOUND)
return()
endif()
find_path(Poco_INCLUDE_DIR Poco/Poco.h)
mark_as_advanced(FORCE Poco_INCLUDE_DIR)
foreach(component ${Poco_FIND_COMPONENTS})
set(component_var "Poco_${component}_LIBRARY")
find_library(${component_var} Poco${component})
mark_as_advanced(FORCE ${component_var})
if(${component_var})
set(Poco_${component}_FOUND TRUE)
list(APPEND Poco_LIBRARIES ${component})
if(NOT TARGET Poco::${component})
add_library(Poco::${component} SHARED IMPORTED)
set_target_properties(Poco::${component} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${Poco_INCLUDE_DIR}
IMPORTED_LOCATION ${${component_var}}
)
endif()
endif()
endforeach()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Poco
FOUND_VAR Poco_FOUND
REQUIRED_VARS Poco_INCLUDE_DIR Poco_LIBRARIES
VERSION_VAR Poco_VERSION
HANDLE_COMPONENTS
)
I installed poco using vcpkg in a directory titled vcpkg. Within the vcpkg directory is the libfranka directory, which houses the CMakeLists.txt file that I compile in Visual Studio. Here is an image of that directory:
Finally, here is the tutorial that I am using: https://frankaemika.github.io/docs/installation_windows.html#building-from-source
EDIT:
Per the link I followed the instructions for solving the build dependencies and here is an image of that:
Then I ran the CMakeLists.txt again and in the CMake Settings this is what I see:
Note also that I ran through the install of poco again and I noticed this and am unsure if it could be the source of the problem or if it means nothing (again, this was the out put after running vcpkg install poco):
After this I still receive the same error.
Does anyone see what it is that I am doing incorrectly?
Thank you!

Can't use protobuf in cmakelists.txt

I am trying to run the example given in protobuf repo here, the c++ version. I have successfully installed the library and am able to run the Makefile. But on running the CMakeLists.txt, I get this error:
CMake Error at CMakeLists.txt:9 (find_package):
Could not find a package configuration file provided by "protobuf" with any
of the following names:
protobufConfig.cmake
protobuf-config.cmake
Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
"protobuf_DIR" to a directory containing one of the above files. If
"protobuf" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeOutput.log".
See also "/home/cortana/Projects/CppProjects/proto/build/CMakeFiles/CMakeError.log".
I have updated my LD_LIBRARY_PATH but this error is still there. How do I remove this error?
EDIT:
CMakeLists.txt:
# Minimum CMake required
cmake_minimum_required(VERSION 2.8.12)
# Project
project(protobuf-examples)
include(FindProtobuf)
# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)
if(protobuf_VERBOSE)
message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
endif()
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
${THIRDPARTY_DIR}/protobuf-3.1.0
)
include_directories(${ProtobufIncludePath})
# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
if(MSVC AND protobuf_MSVC_STATIC_RUNTIME)
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach()
endif()
foreach(example add_person list_people)
set(${example}_SRCS ${example}.cc)
set(${example}_PROTOS addressbook.proto)
#Code Generation
if(protobuf_MODULE_COMPATIBLE) #Legacy Support
protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS})
list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS})
else()
foreach(proto_file ${${example}_PROTOS})
get_filename_component(proto_file_abs ${proto_file} ABSOLUTE)
get_filename_component(basename ${proto_file} NAME_WE)
set(generated_files ${basename}.pb.cc ${basename}.pb.h)
list(APPEND ${example}_SRCS ${generated_files})
add_custom_command(
OUTPUT ${generated_files}
COMMAND protobuf::protoc
ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${CMAKE_CURRENT_SOURCE_DIR} ${proto_file_abs}
COMMENT "Generating ${generated_files} from ${proto_file}"
VERBATIM
)
endforeach()
endif()
#Executable setup
set(executable_name ${example}_cpp)
add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS})
if(protobuf_MODULE_COMPATIBLE) #Legacy mode
target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES})
else()
target_link_libraries(${executable_name} protobuf::libprotobuf)
endif()
endforeach()
EDIT 2:
After trying for 2 hours, I couldn't fix the CMakeLists.txt provided by google examples. I wrote this basic one and it works for me:
PROJECT(protopuff)
CMAKE_MINIMUM_REQUIRED (VERSION 3.5)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER addressbook.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_add add_person.cc)
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}_list list_people.cc)
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_add proto ${PROTOBUF_LIBRARY})
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}_list proto ${PROTOBUF_LIBRARY})
Your problem is here:
find_package(protobuf CONFIG REQUIRED)
The name should start with uppercase: Protobuf. And that is the reason why your version is working; because in there, you have used correct case (last code snippet line 6):
find_package(Protobuf REQUIRED)
Here cmake documentation for find_package
The command searches for a file called <name>Config.cmake or <lower-case-name>-config.cmake for each name specified.
in this thread fraser solved the problem but if you need to develop according to protobuf CMake config and find_package command in CMake for finding protobuf libraries. your protobuf library must be compiled with CMake and do not use configure routine .
after compile protobuf with CMake , a config file named protobuf-config.cmake will be generated into the prefix/lib/CMake/protobuf directory.
The CmakeList.txt that is provided by the OP works on Linux but it does NOT work on Windows.
There is a way to make the actual CMakeList.txt work without any changes. The problem is that it requires the CONFIG parameter and that part is not documented anywhere. We need to provide the path to that config using -Dprotobuf_DIR parameter while generating the project.
On Windows, wherever you have installed protobuf, it will have bin, cmake, include, lib folders. We need to give the path of this cmake folder as an argument, like following:
cmake -G "Visual Studio 16 2019" -A x64 -B _build2 -Dprotobuf_DIR=C:/protobuf/install/cmake
This will build a solution file in the current directory.

Cmake managed C++

I have cli wrapper function which i am trying to configure in cmake. After i generate the project with cmake the generated .proj file does not have the property of clr support is set to no common languaage runtime support. below is my cmake file
# This is the root ITK CMakeLists file.
cmake_minimum_required(VERSION 2.8.9)
if(COMMAND CMAKE_POLICY)
cmake_policy(SET CMP0003 NEW)
endif()
set_target_properties(${TargetName} PROPERTIES COMPILE_FLAGS "/clr")
SET(LINK_LIBRARIES
D:\\2016\\RandomSlicing\\Processing\\lib\\obliquePlane.lib
)
# The header files
SET(HEADERS
ObliquePlaneWrapper.h
obliquePlane.h
)
# The implementation files
SET(SOURCES
ObliquePlaneWrapper.cpp
)
# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
# Add this as include directory
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}
${SOURCE_PATH}
${VXL_INCLUDE_DIRS}
)
# Main library
#ADD_EXECUTABLE(obliquePlane ${HEADERS} ${SOURCES})
ADD_LIBRARY(ObliquePlaneWrapper SHARED ${HEADERS} ${SOURCES})
TARGET_LINK_LIBRARIES(ObliquePlaneWrapper ${LINK_LIBRARIES} ${ITK_LIBRARIES})
I manually set this property in the All_build project and the corresponding .proj file. When i build the project it is searching for the ObliquePlaneWrapper.dll which it should be generating. Is this a problem because of some flag not set for common language runtime support
You can manually supply Compile Flags to specific sources to be compiled with specific flags. This includes \CLR for Visual C++. See example here.
https://cmake.org/pipermail/cmake/2011-April/043773.html

QtCreator CMake Project Auto-Complete Not Always Working?

I have been working on a project that uses cmake as the build system and working in QtCreator just fine. There are sub-projects (some Qt related and others just plain c++) and up until now all of them were able to find header files and hence provide auto-complete functionality.
At some point, I added a Qt project and noticed that my headers for Qt proejcts were not being found by the editor. The really odd thing is they compile and run just fine via QtCreator 'Build->Build All'. It is almost like QtCreator just can't find them for auto-complete.
I looked around and users had problems in earlier versions of QtCreator finding Qt classes, but did not seem to have a problem finding their defined header files.
I put together a simplified "project" and added two fresh Qt Widget Application projects with the main window being a QDialog instead of QMainWindow. I added the following CMakeLists.txt file to each project:
My Standard CMake File For Qt
cmake_minimum_required(VERSION 3.0.2)
project("FooProj")
set(EXECUTABLE_NAME "Foo")
# Find Qt packages.
find_package(Qt5Core)
find_package(Qt5Widgets)
find_package(Qt5Gui)
set(CMAKE_AUTOMOC ON)
set(SOURCES
"src/main.cpp"
"src/Dialog.cpp"
)
set(HEADERS
"includes/Dialog.h"
)
set(UI_FORMS
"forms/Dialog.ui"
)
set(RESOURCES_FILES
"resources/resources.qrc"
)
# Convert Qt UI and resource files to C/C++ files.
qt5_wrap_ui(UI_HEADERS ${UI_FORMS})
qt5_add_resources(RESOURCES ${RESOURCES_FILES})
# Define the executable to be built.
add_executable(${EXECUTABLE_NAME}
${SOURCES}
${HEADERS}
${UI_HEADERS}
${RESOURCES}
)
# Include paths for this project.
target_include_directories(${EXECUTABLE_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
"${CMAKE_CURRENT_BINARY_DIR}"
)
# Link to appropriate libraries.
target_link_libraries(${EXECUTABLE_NAME}
Qt5::Core
Qt5::Widgets
Qt5::Gui
)
# Set distribution location and project structure.
set(DIST_DIR "${CMAKE_BINARY_DIR}/dist/${EXECUTABLE_NAME}")
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY FOLDER "Guis")
# Setup distribution environment and copy final executable.
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${DIST_DIR}
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${EXECUTABLE_NAME}> ${DIST_DIR}
)
# OS specific deployment setup for sytems without Qt installed.
if(WIN32)
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/run.bat" ${DIST_DIR}/${EXECUTABLE_NAME}.bat
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/qt.conf" ${DIST_DIR}
)
elseif(UNIX AND NOT APPLE)
add_custom_command(TARGET ${EXECUTABLE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/QtDeploymentFiles/run.sh" ${DIST_DIR}/${EXECUTABLE_NAME}.sh
)
endif()
Along with this top level CMakeLists.txt:
Top Level CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project("Test")
add_subdirectory("FooProj")
add_subdirectory("BarProj")
Everything makes, compiles, and runs just fine. Auto-complete also works.
I add a Qt library project I need and everything still works fine even when I use the library files. Here is the cmake file for my library:
Qt Library CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project("QtLib")
set(LIB_NAME "QtLib")
# Find Qt packages.
find_package(Qt5Core)
find_package(Qt5Widgets)
set(CMAKE_AUTOMOC ON)
# Set source files to be built.
set(SOURCES
"src/LibFile1.cpp"
"src/LibFile2.cpp"
.
.
.
)
set(HEADERS
"includes/LibFile1Header.h"
"includes/LibFile2Header.h"
"includes/OtherHeaders.h"
.
.
.
)
add_library(${LIB_NAME} ${SOURCES} ${HEADERS})
set_property(TARGET ${LIB_NAME} PROPERTY FOLDER "Libs")
target_include_directories(${LIB_NAME} PRIVATE
"${Qt5Core_INCLUDE_DIRS}"
"${Qt5Widgets_INCLUDE_DIRS}"
"${CMAKE_CURRENT_BINARY_DIR}"
)
target_include_directories(${LIB_NAME} PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/includes"
)
target_link_libraries(${LIB_NAME}
Qt5::Core
Qt5::Widgets
)
Lastly I add one of my real projects (one of the ones that does not auto-complete) The structure is as follows:
-forms
-MainForm.ui
-resources
-resources.qrc
-resource files and dirs
-src
-main.cpp
-MainWindow.cpp
-includes
-MainWindow.h
The CMake file is exactly the same as the ones above. Everything makes, compiles, and runs just fine, but auto-complete stops working. I also noticed that ui_MainWindow.h (the one being auto-generated in my project binary directory) is not found.
The really odd thing is, if I just the exact same files with no changes and just open it as a stand alone project, auto-complete works.
Is there something off about my cmake files or is this a Qt related issue? The same thing happens in VS2013 which makes me think it is not QtCreator related, but I am not sure.
After reading around a little more, it seemed like this might be a QtCreator issue (although I don't know why I had problems in VS2013 when I first tested it). I ended up upgrading my QtCreator to 3.6 and my auto-complete problem went away.