简体   繁体   中英

Using cmake on windows without IDE

this might be a duplicate, but I've already spent a couple of hours searching for an answer... without solution. First of all I know this problem might not exist if I'd use a Linux, but I am on Windows.

I am pretty new to c++ but already got some experience with java and gradle. I try to use cmake just like I am used to use gradle. I already read the cmake wiki, but I either do not find the correct pages or I just don't understand it. Here is my directory structure:

MyProject
-bin
-include
--header1.h
--header2.h
--header3.h
--header4.h
--header5.h
--header6.h
-src
--CMakeLists.txt
--MyProjectConfig.h.in
--impl1.cpp
--impl2.cpp
--impl3.cpp
--impl4.cpp
--impl5.cpp
--impl6.cpp
-main.cpp
-CMakeLists.txt

My CMakeLists.txt in my project folder looks like:

cmake_minimum_required (VERSION 3.14)

project (MyProject)
add_subdirectory(src)

file(GLOB_RECURSE sources      src/*.cpp include/*.h)

# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)


# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/MyProjectConfig.h.in"
  "${PROJECT_BINARY_DIR}/MyProjectConfig.h"
  )

# add the binary tree to the search path for include files
# so that we will find MyProjectConfig.h
include_directories("${PROJECT_BINARY_DIR}")  

install (FILES "${PROJECT_BINARY_DIR}/MyProjectConfig.h"        
         DESTINATION include)

add_executable(MyProject main.cxx ${sources})

# add the install targets
install (TARGETS MyProject DESTINATION bin)

My CMakeLists.txt in the src folder looks like:

cmake_minimum_required (VERSION 3.14)

include_directories(${MyProject_SOURCE_DIR}/MyProject/include)

I use the command in the bin bin directory: cmake -G "MinGW Makefiles" -S ../src

I got 2 questions now:

  1. (How do I tell cmake to always use MinGW? ( I don't want to use -G always)) solved
  2. (The compiled file build\\CMakeFiles\\3.14.0-rc2\\CompilerIdCXX\\a.exe does not have the expected behavior. It should print "Hello world!" and "My Class", while "My Class" is printed from the attribute of a class created from impl1.cpp , however it does nothing.) needs clarification: How do I build a windows .exe-file to ruin on the console?

Edit:

I have learned that I have to call cmake --build . in my bin directory after creating the cmake files. However I just don't get an exe-file. With flag -v I get this output:

"C:\Program Files\CMake\bin\cmake.exe" -SD:\git\MyProject\src -BD:\git\MyProject\bin --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles D:\git\MyProject\bin\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'D:/git/MyProject/bin'
mingw32-make.exe[1]: Nothing to be done for 'all'.
mingw32-make.exe[1]: Leaving directory 'D:/git/MyProject/bin'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles 0 ```
  1. Using the -G option is the standard way of doing it. It prevents you from having to put system-specific settings into your CMake config (like the hardcoded paths to MingW) and lets you use other compilers without having to change build scripts.
  2. The a.exe you started is not your build output. It should be called MyProjectExec.exe . Also, you need to specify all source files in your call to add_executable . add_subdirectory does not automatically add any source files (to what build output should it add them?), it just executes the CMakeLists.txt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM