I just started using CMake so I tried to build a simple hello-world project and got an error.
There is a main.cpp file, which contains this:
int main()
{
return 0;
}
And a CMakeLists.txt, which contains this:
cmake_minimum_required(VERSION "3.18.0")
project("test")
add_executable("${PROJECT_NAME}" "main.cpp")
install(TARGETS "${PROJECT_NAME}" DESTINATION bin)
install(FILES "main.cpp" DESTINATION src)
There also is a Build directory. I build the project from this directory with the following command:
cmake ..
What I eventually get is:
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe - broken
CMake Error at C:/CMake/share/cmake-3.18/Modules/CMakeTestCCompiler.cmake:66 (message):
The C compiler
"C:/MinGW/bin/gcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Projects/trash/checking_out_cmake/Build/CMakeFiles/CMakeTmp
Run Build Command(s):C:/PROGRA~2/GnuWin32/bin/make.exe cmTC_a48fd/fast && C:/PROGRA~2/GnuWin32/bin/make.exe -f CMakeFiles/cmTC_a48fd.dir/build.make CMakeFiles/cmTC_a48fd.dir/build
make.exe[1]: Entering directory `C:/Projects/trash/checking_out_cmake/Build/CMakeFiles/CMakeTmp'
The system cannot find the path specified.
make.exe[1]: *** [CMakeFiles/cmTC_a48fd.dir/testCCompiler.c.obj] Error 1
make.exe[1]: Leaving directory `C:/Projects/trash/checking_out_cmake/Build/CMakeFiles/CMakeTmp'
make.exe: *** [cmTC_a48fd/fast] Error 2
I have got CLion and it works just fine, so I presume this is MinGW compilers what causes the problem.
I have also tried to reinstall the compilers but the error remains.
Be sure not to use non-Latin characters in the absolute path to your working directory. In my case it was Cyrillic "C" as the name of folder.
Related
so what i am doing is that i am having 2 targets in a project, one is program for embedded device that i am crosscompiling, and the second are tests. I need to build them with 2 different compilers.
To main project i am passing arm compiler path by file toolchain.cmake and its working fine.
But right now i would like to build also the tests without having to add path to mingw64/bin to my environment variables (yes its working if i do that).
Test project is added as:
ExternalProject_Add(${UNIT_TEST_TARGET}
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test/
PREFIX ${UNIT_TEST_TARGET}
CMAKE_ARGS ${GTEST_ARGS}
BUILD_ALWAYS TRUE
INSTALL_COMMAND cmake -E echo "Skipping install step."
EXCLUDE_FROM_ALL TRUE)
So right now i would like to pass to this external project file toolchaintests.cmake which looks like :
set(CMAKE_C_COMPILER <my_path_here>/mingw64/bin/gcc.exe)
set(CMAKE_CXX_COMPILER <my_path_here>/mingw64/bin/g++.exe)
with:
list(APPEND GTEST_ARGS "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/toolchaintests.cmake")
and if i do this cmake can't recognize the compilers:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: <my_path_here>/mingw64/bin/gcc.exe
-- Check for working C compiler: <my_path_here>/mingw64/bin/gcc.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.24/Modules/CMakeTestCCompiler.cmake:69 (message):
The C compiler
" <my_path_here>/mingw64/bin/gcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: <my_path_here>/out/build/tests/src/tests-build/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Ninja/ninja-win/ninja.exe cmTC_a1c48 && [1/2] Building C object CMakeFiles\cmTC_a1c48.dir\testCCompiler.c.obj
FAILED: CMakeFiles/cmTC_a1c48.dir/testCCompiler.c.obj
<my_path_here>\mingw64\bin\gcc.exe -o CMakeFiles\cmTC_a1c48.dir\testCCompiler.c.obj -c <my_path_here>\out\build\tests\src\tests-build\CMakeFiles\CMakeTmp\testCCompiler.c
ninja: build stopped: subcommand failed.
Can i do this somehow else?
I installed vcpkg on macOS and I'm trying to build a simple library that depends on fmt, which I installed with vcpkg.
mylib.h
float add(float a, float b);
mylib.cpp
#include "mylib.h"
#include <iostream>
#include <fmt/core.h>
float add(float a, float b)
{
fmt::print("Hello MYLIB, world!\n");
return (a + b);
}
CMakeLists.txt contents:
cmake_minimum_required(VERSION 3.19.1)
project(MYLIB)
find_package(fmt REQUIRED)
add_library(mylib mylib.cpp)
Then
user#users-MacBook-Pro build % cmake -B . -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake -S ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user/mylib/build
user#users-MacBook-Pro build % make
Scanning dependencies of target mylib
[ 50%] Building CXX object CMakeFiles/mylib.dir/mylib.cpp.o
/Users/user/mylib/mylib.cpp:5:10: fatal error: 'fmt/core.h' file not found
#include <fmt/core.h>
^~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/mylib.dir/mylib.cpp.o] Error 1
make[1]: *** [CMakeFiles/mylib.dir/all] Error 2
make: *** [all] Error 2
What am I missing?
I tried the same on Windows and it works fine. On Windows though we run vcpkg integrate install which does not exist on macOS. Is this related to the problem?
Looks like it's necessary to
include_directories(~/vcpkg/installed/x64-osx/include)
You are using the variable CMAKE_TOOLCHAIN_FILE incorrectly. set(CMAKE_TOOLCHAIN_FILE ... in CMakeLists.txt has no effect. The variable should be set on the command line, see the manuals CMAKE_TOOLCHAIN_FILE, Using vcpkg with CMake
cmake .. -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake
The file CMakeLists.txt is also wrong, find_package(fmt REQUIRED) is missing, that should download and install fmt by invoking vcpkg install fmt under the hood.
After all you should link your project with the lib
target_link_libraries(MYLIB PRIVATE fmt::fmt)
To be honest im not a fan of Qt Creator. So I decided to use another IDE like CLion. I researched and found out that Clion doesnt support qmake, but it is possible to get a Qt-Project to run with cmake.
Thats my CMakeList. CLion don't give me any error when i save this make-file.
cmake_minimum_required(VERSION 3.8)
project(Qt_CmakeTest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
add_executable(Qt_CmakeTest ${SOURCE_FILES})
target_link_libraries(Qt_CmakeTest Qt5::Core Qt5::Widgets)
to test if it worked I created a simple cpp.
#include <iostream>
#include <QApplication>
using namespace std;
int main(int argc, char **argv)
{
QApplication app(argc,argv);
return app.exec();
}
I tried to implement it following these instructions
How to configure CLion IDE for Qt Framework?
I have to use the cmake binary that qt provides. So I set the Cmake Option to:
-DCMAKE_PREFIX_PATH=C:\Qt\Qt5.9.1\5.9.1\mingw53_32\lib\cmake
But if I try to compile it it gives me this error:
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build
C:\Users\Marcel\Desktop\Projekte\Qt\Qt-CmakeTest\cmake-build-debug --target Qt_CmakeTest -- -j 2
[ 50%] Linking CXX executable Qt_CmakeTest.exe
CMakeFiles\Qt_CmakeTest.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:8: undefined reference to `_imp___ZN12QApplication4execEv'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationD1Ev'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Qt_CmakeTest.exe] Error 1
CMakeFiles\Qt_CmakeTest.dir\build.make:99: recipe for target 'Qt_CmakeTest.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Qt_CmakeTest.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Qt_CmakeTest.dir/rule' failed
Makefile:117: recipe for target 'Qt_CmakeTest' failed
mingw32-make.exe[2]: *** [CMakeFiles/Qt_CmakeTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Qt_CmakeTest.dir/rule] Error 2
mingw32-make.exe: *** [Qt_CmakeTest] Error 2
I downloaded different versions of Qt because Qt 5.9 uses Mingw 5.3 and CLion says I'm using Mingw 5.0 and at the beginning I used Qt 5.6 and it used Mingw 4.9. I can't really figure out why it is not working.
A clean CMake Build looks like this
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Marcel\Desktop\Projekte\Qt\Qt-CmakeTest
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/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:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/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: C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/cmake-build-debug
[Finished]
Somehow there was no PATH to the Qt binary. I have added the PATH and rebuild the cmake Project and it worked. So it was just a matter of the path and of the versions.
I am trying to setup cmake using Windows 10 using MinGW. I have included the path c:/MinGW/bin in my system path and environment path settings. I have removed sh.exe from my path (although, i would love to be able to keep this if possible).
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/gcc.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Output
C:\School\athabascua\data structures\ass1>cmake -g "MinGW Makefiles" .
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_b3144\fast"
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: C:/School/athabascua/data structures/ass1/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_b3144\fast"
Generator: execution of make failed. Make command was: "nmake" "/NOLOGO"
"cmTC_b3144\fast"
It seems that the GNU compilers are identified but don't seem to work. Any help would be much appreciated. I am trying to avoid using Cygwin.. but almost ready to go that route in a sec here.
To fix the problem I was having, I had to do two things.
use the command cmake -G "MinGW Makefiles" . (capital -G on windows)
update my CMakeLists.txt file to use gcc for C compiler and g++ for C++ compiler
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)
I have the following compile time error I cannot find the reason of:
fatal error: mysql_connection.h: No such file or directory
I am using cmake, and these are the CMakeLists.txt files:
# Top level CMakeLists.txt - MyProg
cmake_minimum_required (VERSION 2.6)
set (PROJECT_NAME "MyProg")
### Out-of-tree directories
set (UTILITIES_DIR "~/utilities")
### Configure header file to pass CMake's settings to the source code
configure_file (
"Config.h.in"
"${PROJECT_SOURCE_DIR}/Config.h"
)
add_subdirectory (src "${CMAKE_CURRENT_BINARY_DIR}/obj")
add_subdirectory (${UTILITIES_DIR} "${CMAKE_CURRENT_BINARY_DIR}/obj/external/utilities")
and
# Source level CMakeLists.txt - MyProg/src
### MySQL Connector/C++ ###
set (MYSQLCONNECTORCPP_ROOT_DIR "~/3rdParty/mysql-connector-c++-1.1.0")
### Include paths ###
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
include_directories (${UTILITIES_DIR})
include_directories (${MYSQLCONNECTORCPP_ROOT_DIR})
include_directories (/usr/local/include)
link_directories (/usr/local/lib)
link_directories (${MYSQLCONNECTORCPP_ROOT_DIR}/driver)
link_directories (/usr/lib64/mysql/)
link_directories (/usr/lib64/)
link_directories (/usr/local/mysql/lib/)
add_executable (myprog
entrypoint.cpp
MyProg.cpp
MyProg_test.cpp
${UTILITIES_DIR}/DBInterface.cpp
)
target_link_libraries (myprog mysqlcppconn-static mysqlclient)
This is the output from cmake (out of source build):
> cmake ../MyProg/
-- The C compiler identification is GNU 4.7.1
-- The CXX compiler identification is GNU 4.7.1
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: ~/MyProg_prj/Debug
And this is the fatal error I get from make:
> make
Scanning dependencies of target myprog
[ 8%] Building CXX object obj/CMakeFiles/myprog.dir/entrypoint.cpp.o
In file included from ~/MyProg_prj/MyProg/src/entrypoint.cpp:18:0:
~/utilities/DBInterface.hpp:18:30: fatal error: mysql_connection.h: No such file or directory
compilation terminated.
make[2]: *** [obj/CMakeFiles/myprog.dir/entrypoint.cpp.o] Error 1
make[1]: *** [obj/CMakeFiles/myprog.dir/all] Error 2
make: *** [all] Error 2
The mysql_connection.h file is in the directory specified in the CMakeLists.txt file.
This problem happened after I upgraded Linux (before it worked correctly), but this should not be the reason. The PATH should contain everything needed.
Thank you.
Platform: Linux (OpenSuse), GCC 4.7.1, cmake, MySQL Connector C++ 1.1.0
You are most likely missing the 'libmysqlcppconn-dev' library. Once installed, you shouldn't be seeing this error.
Before updating the system, in the source code I included the MySQL header like this:
#include <mysql_connection.h>
Now I have to specify the subdirectory:
#include <driver/mysql_connection.h>
Something must have changed in how the paths are set...