Good day!
Let us have a source file main.cpp and a CMakeLists.txt file containing the next text:
cmake_minimum_required(VERSION 2.6)
project(tmp)
set(CMAKE_CXX_FLAGS "-Wall")
add_executable(tmp.elf main.cpp)
Let's say the main.cpp file contains a simple "Hello, World!" program:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
We can build the project with cmake CMakeLists.txt && make. Then we'll just get the tmp.elf file which we can just run. Or we can get no tmp.elf file and assume that something is wrong with the main.cpp source file (assuming the compiler and cmake are installed properly on the building system).
So, the question is: how can we do the same on the Windows machine? E.g. we will get the tmp.vcproj file after running cmake CMakeLists.txt and then we need to build it somehow. How the build process can be performed using command-line? (Java's Process.start(), actually :-P )
You can start the build in a platform and CMake generator independent fashion by invoking cmake with the --build option:
cmake --build .
For multi-configuration generators, you can specify the configuration in the following way:
cmake --build . --config Release
Also see the documentation.
Related
Summary
In Windows, CLion doesn't complain anything after cmake and before makefile.
All code seems to link correctly without error. I am able to see the reference, documents, linter and jump into cv::Mat or opencv.hpp header file with ctrl RMB.
CMake seems to correctly generate make files without error.
But the compile error occurs: undefined reference to OpenCV methods.
my setup
CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(test)
# Set compile version
set(CMAKE_CXX_STANDARD 17)
add_executable(test ./test.cpp )
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " libraries: ${OpenCV_LIBRARIES}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
target_link_libraries(test ${OpenCV_LIBRARIES})
The output seems correct
"D:\Program Files\JetBrains\CLion 2021.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\repo\CS5330\test
-- OpenCV library status:
-- version: 4.5.5
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
-- include path: /path/to/scoop/apps/opencv/current/include
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/test/cmake-build-debug
[Finished]
And this generates CMakeCache.txt, which includes,
//The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH= /path/to/scoop/apps/opencv/current/x64/vc15/lib
//Details about finding OpenCV
FIND_PACKAGE_MESSAGE_DETAILS_OpenCV:INTERNAL=[/path/to/scoop/apps/opencv/current][v4.5.5()]
As you see, system-wide environment variable OpenCV_DIR has been already set and read correctly by CLion.
Here is a simple test code, but failed to run
#include <opencv2/opencv.hpp>
int main() {
cv::Mat img = cv::imread("./test.jpg", -1);
cv::imshow("Mon image", img);
cv::waitKey(0);
return 0;
}
I also installed msys2 from winget, and from msys2 installed clang, make, MinGW-w64 GDB,cmake. Following this tutorial.
And tested through that toolchain instead of CLion bundled toolchain, it returns the same result.
OpenCV binary is from scoop.
For some reason, it has the same problem as one from homebrew. In opencv.hpp, includes headers are incorrect due to file hierarchy that opencv.hpp is inside opencv2 folder.
I changed all #include "opencv2/header.hpp" to #include "header.hpp", but it doesn't help, and vice versa.
Can't figure out the reason for hours.. Any help will be appreciated.
I am trying to build a very simple 'hello triangle' OpenGL application using CMake. I want to use find_package(GLEW) and the resulting imported target to link with Glew. When I run the make command, I am getting a 'No rule to make target' error.
According to the CMake documentation
https://cmake.org/cmake/help/latest/module/FindGLEW.html
https://cmake.org/cmake/help/latest/command/find_package.html
I can use find_package(GLEW) to return a target GLEW::GLEW which I will be able to use to link the Glew library to my code. When I run cmake .. everything builds and it reports that the glew-config.cmake file has been found.
-- Found GLEW: /usr/local/lib/cmake/glew/glew-config.cmake
I then run make and receive this error
$ make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
make[2]: *** No rule to make target `/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework', needed by `test'. Stop.'
I am running Mojave 10.14 and used homebrew to install cmake and glew which are both up to date. The path returned in the error above does not exist and I don't know where it is coming from.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(test)
find_package(GLEW REQUIRED)
add_executable(test main.cpp)
target_link_libraries(test GLEW::GLEW)
Here is my source code:
#include <GL/glew.h>
#include <iostream>
int main(int argc, char **argv){
std::cout << "Glew Test" << std::endl;
}
As you can see it is the bare minimum code needed to simply link Glew.
The problem seems to be similar to this question:
CMake FIND_PACKAGE succeeds but returns wrong path
However, in that case there was a solution using a command line flag specific to Boost. The only reference I have found regarding FindGLEW and problems with macOS is here:
https://gitlab.kitware.com/cmake/cmake/issues/19542
But that seems to have been resolved.
The problem I am having seems to be this path that FindGLEW.cmake is returning:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework
I have looked in the actual FindGLEW.cmake file to see if I could find the source of this path but I'm not an expert with CMake so got lost pretty quickly. How could I find the origin of this path? Are there any CMake flags or variables I can set to work around this similar to the Boost question above?
I have successfully linked with Glew using this CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(test)
find_library(GLEW_DYLIB GLEW "/usr/local/Cellar/glew/2.1.0/lib")
if(NOT GLEW_DYLIB)
message(FATAL_ERROR "Glew not found")
endif()
add_library(Glew_target SHARED IMPORTED)
set_property(TARGET Glew_target PROPERTY IMPORTED_LOCATION "/usr/local/Cellar/glew/2.1.0/lib/libGLEW.dylib")
set_property(TARGET Glew_target PROPERTY IMPORTED_IMPLIB ${GLEW_DYLIB})
include_directories(
"/usr/local/Cellar/glew/2.1.0/include/"
)
add_executable(test main.cpp)
target_link_libraries(test Glew_target)
However, I would prefer to keep things as simple as possible with find_package() as that is definitely the cleaner method.
EDIT: SOLVED - see Tsyvarev's comment below clarifying the nature of the glew-config file that CMake was finding above. I removed this file and rebuilt the project with no problems.
i'm trying to build a simple cmake application to test the coverage functionality offer by clang in windows (in linux all work ok).
Environment is:
Clang version in use: 7.0
And using LLVM toolset with Visual Studio 2017 build tools
cmake 3.12 with ninja generator
What I'm doing is simple:
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(test)
add_compile_options(--coverage)
add_executable(${PROJECT_NAME}
main.cpp
)
the main.cpp is as simple as it can get:
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
std::cout << "\ndone.\n";
return 0;
}
using the build tools environments vcvarsall.bat i initialize the environement to run and compile with clang, like that:
mkdir build
cd build
cmake -GNinja -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_LINKER=lld-link ..
cmake --build .
and the following errors hapear:
lld-link.exe: error: undefined symbol: __llvm_profile_runtime
>>> referenced by src\app\CMakeFiles\app.dir\main.cpp.obj:(__llvm_profile_runtime_user)
lld-link.exe: error: undefined symbol: __llvm_profile_register_function
>>> referenced by src\app\CMakeFiles\app.dir\main.cpp.obj:(__llvm_profile_register_functions)
>>> referenced by src\app\CMakeFiles\app.dir\main.cpp.obj:(__llvm_profile_register_functions)
What i'm doing wrong or what i need to link to?
Thanks
Update: with Fred input I realized what library I should be linking to, and if I add the following:
link_libraries("C:/Program Files/LLVM/lib/clang/7.0.0/lib/windows/clang_rt.profile-x86_64.lib")
everything works... but is this really the solution for my problem?
According to these Notes and Notes and simple example and another example you need to do this differently.
target_compile_options(${PROJECT_NAME} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
target_link_libraries(${PROJECT_NAME} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
I haven't tested the syntax or anything.
From what I gather is that --coverage is an older flag of making gcov compatible data. The LLVM documents give an example of -fprofile-instr-generate -fcoverage-mapping at compile and link command there is no documentation of --coverage. One of the examples say you need to link with --coverage the other example says you need to link with -fprofile-instr-generate -fcoverage-mapping and it will link in the profile libraries as needed.
I hope someone is able to help me:
I try to use the cmake plugin in a jenkins server to create a hex file, but i don't know my fault.
I also try to run the example of this site: http://robot-develop.org/archives/2952 but if i use the "make" command, i get this error message:
make: *** No targets specifed and no makefile found. Stop
Here is my code:
CMakeLists.txt:
cmake_minimum_required (VERSION 2.6.0)
INCLUDE("C:/Users/name/Desktop/example/Code/test_crosscompile.cmake")
INCLUDE("C:/Users/name/Desktop/example/Code/macro.cmake")
project (Code)
MESSAGE(STATUS "\n -------------------------------->Creating Test-Projekt...")
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Release ... FORCE )
ENDIF()
SET(MAIN_FILE
class.c)
# add the executable
add_executable(Code ${MAIN_FILE})
#makro
AVR_CREATE_HEX(class)
makro.cmake:
# create avr hex
MACRO(AVR_CREATE_HEX name)
ADD_CUSTOM_COMMAND(TARGET ${name} POST_BUILD COMMAND avr-objcopy ARGS -O ihex -R.eeprom ${name} "C:/Users/name/Desktop/example/Code/class.hex")
MESSAGE(STATUS "\n ----------JONAS---------------------->Macro")
ENDMACRO(AVR_CREATE_HEX)
test_crosscompile.cmake:
SET(CMAKE_SYSTEM_NAME "Windows")
SET(CMAKE_C_COMPILER avr-gcc)
SET(CMAKE_CXX_COMPILER avr-g++)
SET(CSTANDARD "-std=gnu99")
SET(CDEBUG "-gstabs")
SET(CWARN "-Wall -Wstrict-prototypes")
SET(CTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums")
SET(COPT "-Os")
SET(CMCU "-mmcu=atmega32")
SET(CDEFS "-DF_CPU=12000000UL")
SET(CFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${CINCS} ${COPT} ${CWARN} ${CSTANDARD} ${CEXTRA}")
SET(CXXFLAGS "${CMCU} ${CDEFS} ${CINCS} ${COPT}")
SET(CMAKE_C_FLAGS ${CFLAGS})
SET(CMAKE_CXX_FLAGS ${CXXFLAGS})
I use the cmake GUI, but i don't know how to use it.
I try as generator visual studio 10 (specify toolchain file for cross compiling), but it generates no makefile or hex file.
If i choose MinGW Makefiles as the generator, i get the error:
The C compiler "C:/WinAVR-20100110/bin/avr-gcc.exe" is not able to compile
a simple test program.
I hope somebody can help me, THANKS!
You have the CMAKE_SYSTEM_NAME set to windows, you need to set this to generic to enable Cross compiling eg:
SET(CMAKE_SYSTEM_NAME Generic)
I used to use Xcode to build and run C++ program.
I use command line to compile the same source code in my Xcode project.
Compiling a individual .cpp file is OK.
Compiling more complicated project(more than one file) is NOT OK.
I have tried gcc, g++, clang, clang++.
The main problem is undefined symbol.
Could you show how to compile complicated project (more than one file) by command line?
From your description, it sounds like you're not using the -c (compile, but don't link) flag. Steps to build your project:
Compile all of the source files:
c++ -c file1.cpp
c++ -c file2.cpp
c++ -c file3.cpp
Link your final executable:
c++ file1.o file2.o file3.o
You can use an optional -o flag to specify the output program name. It probably defaults to a.out.
It would be better to use some kind of build tool (like make, for example) to automate this process, or you'll find it to be very error prone.