Trouble launching QApplication class with VSCode & C++ - c++

I am new to C++ as well as VSCode, so this issue might have a really simple answer I'm just overlooking. I'm trying to build an application using the Qt library after having loved its PyQt variant, but I cannot get the simplest possible app to launch from the VSCode terminal.
Here's the program I try to build and run:
// main.cpp - I copied and pasted the Qt documentation example after I had Qt import issues
// (which I have since resolved)
#include <iostream>
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
std::cout << "So far";
QApplication qga (argc, argv);
QPushButton btn ("ayo");
btn.show();
return qga.exec();
}
Here's the CMakeLists.txt file that builds the app:
cmake_minimum_required(VERSION 3.16)
project(trial VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "C:/Qt/6.3.0/mingw_64/")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets Gui Core)
qt_standard_project_setup()
add_executable(trial main.cpp)
target_link_libraries(trial PRIVATE Qt6::Widgets Qt6::Gui Qt6::Core)
include(CPack)
These are the components I'm using:
VS Code 2017
CMake (extension)
CMake Tools (extension)
Qt tools (extension)
GCC 8.1.0x86_64-w64-mingw32 compiler
Qt 6.3.0
What confuses me is that the program builds seemingly without error - the program finds Qt and throws no error when compiling. But whenever I launch it from the VS Code terminal, nothing happens. No statement is printed nor is any window launched.
In case it helps, here's the output when I try to launch it:
[variant] Loaded new set of variants
[kit] Successfully loaded 5 kits from C:\Users\astro\AppData\Local\CMakeTools\cmake-tools-kits.json
[proc] Executing command: C:\MinGW64\bin\gcc.exe -v
[main] Configuring folder: qttrial
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\MinGW64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\MinGW64\bin\g++.exe -Sc:/Users/astro/Desktop/Git/qttrial -Bc:/Users/astro/Desktop/Git/qttrial/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 8.1.0
[cmake] -- The CXX compiler identification is GNU 8.1.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/MinGW64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/MinGW64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Looking for pthread.h
[cmake] -- Looking for pthread.h - found
[cmake] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
[cmake] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
[cmake] -- Check if compiler accepts -pthread
[cmake] -- Check if compiler accepts -pthread - yes
[cmake] -- Found Threads: TRUE
[cmake] -- Performing Test HAVE_STDATOMIC
[cmake] -- Performing Test HAVE_STDATOMIC - Success
[cmake] -- Found WrapAtomic: TRUE
[cmake] -- Found WrapVulkanHeaders: C:/VulkanSDK/1.3.211.0/Include
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: C:/Users/astro/Desktop/Git/qttrial/build
[main] Building folder: qttrial trial
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/astro/Desktop/Git/qttrial/build --config Debug --target trial -j 14 --
[build] [ 25%] Automatic MOC and UIC for target trial
[build] [ 25%] Built target trial_autogen
[build] [ 50%] Building CXX object CMakeFiles/trial.dir/trial_autogen/mocs_compilation.cpp.obj
[build] [ 75%] Building CXX object CMakeFiles/trial.dir/main.cpp.obj
[build] [100%] Linking CXX executable trial.exe
[build] [100%] Built target trial
[build] Build finished with exit code 0
Terminal results:
PS C:\Users\astro\Desktop\Git\qttrial\build> ."C:/Users/astro/Desktop/Git/qttrial/build/trial.exe"
PS C:\Users\astro\Desktop\Git\qttrial\build>
I would be immensely grateful to anyone who can point me in a direction to solve this. Given that there are no error messages, I'm lost as to where to start. If any of you guys need any more information, I'll be happy to supply it. :)

Related

CMake add_executable() failing to make executable and 3rd party library failing to untar

Problem Background
I am trying to incorporate UnitTest++ into my linux-based C++17 project template. I have done this on windows pretty easily, but I am running into major issues on linux for some reason.
I used Cmake 3.21 on Windows and I am currently using CMake 3.17.5 on Linux.
I downloaded the UnitTest++ source code from the UnitTest++ Github. and proceeded to throw the tar.gz into my 3rdParty folder in my ProjectTemplate. I have had major trouble getting it to build. So I have stripped out everything except for the one 3rd party library I added and the things that folder affects. So at this point all the main() functions are very simple. I am just expecting to see executables that do nothing.
Problem Points
The main executable gets made, but the unit testing executable
(testExecutable) does not get made.
The build fully finishes with no errors even though an executable
failed to be made.
The UnitTest++ 3rd party library fails to untar with no errors
output.
I have included the build output.
Why is the testExecutable not getting made? More importantly, why is my UnitTest++ failing to untar?
Directory Structure
ProjectTemplate
+3rdParty
-CMakeLists.txt (3rd party cmake)
-unittest-cpp-2.0.0.tar.gz
+build
+src
-ClassTemplate.cpp (just a plain class file - compiles fine)
-ClassTemplate.hpp (just a plain class file - compiles fine)
-main.cpp (basic int main function)
+UnitTests
-CMakeLists.txt (unit test cmake)
-UnittestcppMain.cpp (basic int main function)
-CMakeLists.txt (root cmake)
Build Output
Output from cmake generation / configure
[main] Configuring folder: ProjectTemplate
[proc] Executing command: /usr/bin/cmake3 --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/opt/rh/devtoolset-8/root/usr/bin/gcc -DCMAKE_CXX_COMPILER:FILEPATH=/opt/rh/devtoolset-8/root/usr/bin/g++ -S/path/to/ProjectTemplate -B/path/to/ProjectTemplate/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The CXX compiler identification is GNU 8.3.1
[cmake] -- Check for working CXX compiler: /opt/rh/devtoolset-8/root/usr/bin/g++
[cmake] -- Check for working CXX compiler: /opt/rh/devtoolset-8/root/usr/bin/g++ - works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] Made it to this
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /path/to/ProjectTemplate/build
output from build
[main] Building folder: ProjectTemplate
[build] Starting build
[proc] Executing command: /usr/bin/cmake3 --build /path/to/ProjectTemplate/build --config Debug --target ExecutableName -j 18 --
[build] Scanning dependencies of target ExecutableLib
[build] [ 25%] Building CXX object CMakeFiles/ExecutableLib.dir/src/ClassTemplate.cpp.o
[build] [ 50%] Linking CXX static library libExecutableLib.a
[build] [ 50%] Built target ExecutableLib
[build] Scanning dependencies of target ExecutableName
[build] [ 75%] Building CXX object CMakeFiles/ExecutableName.dir/src/main.cpp.o
[build] [100%] Linking CXX executable ExecutableName
[build] [100%] Built target ExecutableName
[build] Build finished with exit code 0
UPDATE
Thanks to 273K a solution was pointed out. I had accidentally switched my target from "All" to "ExecutableName" in VSCode. I have included a screenshot of the problem area and what it had to be set to in order to avoid this problem for future users who might be using VSCode and stumble on this question.
This is the bottom edge of VSCode, this is where the CMake extension puts it build options.
Somehow you set --target ExecutableName, that limits the build to a single target, the main executable ExecutableName. Try to select another target, or let it unset.

Can't use vcpkg on linux

Here is my CMakelists.txt:
cmake_minimum_required(VERSION 3.0)
SET(CMAKE_TOOLCHAIN_FILE "/home/xxx/vcpkg/scripts/buildsystems/vcpkg.cmake")
project(test)
find_package(unofficial-sqlite3 CONFIG REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
I'm trying to use vcpkg through SET(CMAKE_TOOLCHAIN_FILE ${vcpkg_root}),but it seems that it doesn't work.Here is the error:
[cmake] CMake Error at CMakeLists.txt:5 (find_package):
[cmake] Could not find a package configuration file provided by
[cmake] "unofficial-sqlite3" with any of the following names:
[cmake]
[cmake] unofficial-sqlite3Config.cmake
[cmake] unofficial-sqlite3-config.cmake
I have verify that unofficial-sqlite3-config.cmake does exists in
/home/xxx/vcpkg/packages/sqlite3_x64-linux/share/unofficial-sqlite3/unofficial-sqlite3-config.cmake
I'm using WSL and vscode as IDE, Now I have no idea how to fix it.
I could not reproduce your issue using the following basic CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(test)
find_package(unofficial-sqlite3 REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)
I installed sqlite3 via vcpkg install sqlite3 and ran the build via
$ echo 'int main() { return 0; }' > main.cpp
$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
As you can see, this worked flawlessly. I'm not sure whether setting your CMake policy level to the downright ancient 3.0 is what matters, or if you're using an old vcpkg checkout (I used the most recent commit at time of writing).

Windows not able to find FLEX_LIBRARIES

When using this CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(project_c)
set(CMAKE_CXX_STANDARD 11)
set(project_name project_c)
find_package(BISON)
find_package(FLEX)
BISON_TARGET(parser parser.y ${CMAKE_SOURCE_DIR}/parser.cpp)
FLEX_TARGET(lexer lexer.l ${CMAKE_SOURCE_DIR}/lexer.cpp)
ADD_FLEX_BISON_DEPENDENCY(lexer parser)
add_executable(${project_name} ${BISON_parser_OUTPUTS} ${FLEX_lexer_OUTPUTS})
target_include_directories(${project_name} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(${project_name} ${FLEX_LIBRARIES})
CMake complains about
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
FL_LIBRARY (ADVANCED)
linked by target "project_c" in directory D:/asant/workspace/CLionProjects/project_c
I've tried to copy the winflex folder inside the project folder but that won't help anyway. This proposed solution isn't working.
This is the complete CMake log
"C:\Program Files\JetBrains\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\asant\workspace\CLionProjects\project_c
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Check for working C compiler: C:/Qt/Tools/mingw810_64/bin/gcc.exe
-- Check for working C compiler: C:/Qt/Tools/mingw810_64/bin/gcc.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Qt/Tools/mingw810_64/bin/g++.exe
-- Check for working CXX compiler: C:/Qt/Tools/mingw810_64/bin/g++.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found BISON: C:/Program Files (x86)/win_flex_bison-2.5.23/win_bison.exe (found version "3.7.1")
-- Found FLEX: C:/Program Files (x86)/win_flex_bison-2.5.23/win_flex.exe (found version "2.6.4")
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
FL_LIBRARY (ADVANCED)
linked by target "project_c" in directory D:/asant/workspace/CLionProjects/project_c
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
[Failed to reload]

Detecting compiler ABI failed by CMAKE

There is a CMake project simple enough but not compiling at all with Visual Studio Code. The source:
// main.cpp
#include <iostream>
int main(int argc, char * argv){
return 0;
}
The CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(test VERSION 0.1.0)
add_executable(test main.cpp)
CMake Tools in vscode build with errors in prompt as below:
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - failed
[cmake] -- Check for working C compiler: /bin/gcc-9
[cmake] -- Check for working C compiler: /bin/gcc-9 - works
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] CMake Error at /usr/local/share/cmake-3.18/Modules/CMakeTestCCompiler.cmake:82
(configure_file):
[cmake] configure_file Problem configuring file
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:2 (project)
[cmake]
[cmake]
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - failed
[cmake] -- Check for working CXX compiler: /bin/g++-9
[cmake] -- Check for working CXX compiler: /bin/g++-9 - works
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] CMake Error at /usr/local/share/cmake-3.18/Modules/CMakeTestCXXCompiler.cmake:75
(configure_file):
[cmake] configure_file Problem configuring file
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:2 (project)
The CMakeError.log:
Cannot copy output executable
'/data/solution/projects/test/build/CMakeFiles/CMakeTmp/cmTC_efc0d'
to destination specified by COPY_FILE:
'/data/solution/projects/test/build/CMakeFiles/3.18.4/CMakeDetermineCompilerABI_C.bin'
Cannot copy output executable
'/data/solution/projects/test/build/CMakeFiles/CMakeTmp/cmTC_26771'
to destination specified by COPY_FILE:
'/data/solution/projects/test/build/CMakeFiles/3.18.4/CMakeDetermineCompilerABI_CXX.bin'
A selection of the CMakeOutput.log:
The system is: Linux - 5.4.0-52-generic - x86_64
Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded.
Compiler: /bin/gcc-9
Build flags:
Id flags:
The output was:
0
Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "a.out"
The C compiler identification is GNU, found in
"/data/solution/projects/test/build/CMakeFiles/3.18.4/CompilerIdC/a.out"
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded.
Compiler: /bin/g++-9
Build flags:
Id flags:
The output was:
0
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.out"
The CXX compiler identification is GNU, found in
"/data/solution/projects/test/build/CMakeFiles/3.18.4/CompilerIdCXX/a.out"
Determining if the C compiler works passed with the following output:
Change Dir: /data/solution/projects/test/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make cmTC_3f39e/fast && /usr/bin/make -f
CMakeFiles/cmTC_3f39e.dir/build.make CMakeFiles/cmTC_3f39e.dir/build
make[1]: Entering directory '/data/solution/projects/test/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_3f39e.dir/testCCompiler.c.o
/bin/gcc-9 -o CMakeFiles/cmTC_3f39e.dir/testCCompiler.c.o -c
/data/solution/projects/test/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_3f39e
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_3f39e.dir/link.txt --verbose=1
/bin/gcc-9 -rdynamic CMakeFiles/cmTC_3f39e.dir/testCCompiler.c.o -o cmTC_3f39e
make[1]: Leaving directory '/data/solution/projects/test/build/CMakeFiles/CMakeTmp'
The project doesn't build anymore when the OS was upgraded from 18.04 to 20.04 all in a sudden, with later version (9.3) of GCC toolchain. The
a.out
exists under CMAKE building directory, but there is no output executable ABI testing program to copy to anyway, as we can tell from the above logs. Is there anyone to help me finding the real cause? Thank you.
Solution: OK. There is an issue I could have noted, thanks to a comment. The project is under directory mounted to FAT32 file system where permission settings are tricky to configure. A correct /etc/fstab file:
UUID=3FCE-E5CB /data vfat rw,user,exec,utf8,uid=jack,gid=jack 0 0
The CMAKE is not able to access to file system with permission granted. Solution is to edit /etc/fstab accordingly.

Setting up Project with Clion and SFML works, but only the first time

So, i set up my Cloin/SFML project like this : configure SFML for clion (windows)
and added the SFML_ROOT variable, and then it works exactly once, and every time i try too run it after the first, i get this error(gam is the project name, it is set correctly in the CMakeLists.txt file):
mingw32-make.exe: *** No rule to make target 'gam'. Stop.
How do i get it to work more than once?(It might be something as stupid as me clicking the wrong button to run this thing. I´m new to Clion and cmake)
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(gam)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(gam ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(gam ${SFML_LIBRARIES})
endif()
output of CMake when project is first initialized:
"C:\Program Files\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\programierzeug\c++\test
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc.exe
-- Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/programierzeug/c++/test/cmake-build-debug
[Finished]
output of CMake when reloading project:
"C:\Program Files\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\programierzeug\c++\test
-- Configuring done
-- Generating done
-- Build files have been written to: D:/programierzeug/c++/test/cmake-build-debug
[Finished]