0

Currently, I have a CMakeLists.txt file in the main folder that has the following code in it:

cmake_minimum_required(VERSION 3.5)

SET(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE)
SET(CMAKE_TOOLCHAIN_FILE ${PROJECT_SOURCE_DIR}/ToolChain.cmake)

project(Blinky)

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

...

When I run it, it outputs first for Visual Studio to the source directory. When I run it the second time, it outputs the corresponding minGW makefiles but still to the source directory and not the bin folder. Is there any way to configure it to build for MinGW Makefile directly and to the correct output folder?

I'm running the script on a command line prompt with the following line of code from the source directory folder:

cmake CMakeLists.txt
  • 1
    Why you use such strange way? Why not just as usual `mkdir bin` `cd bin` `cmake ..` ? – fghj Apr 30 '16 at 21:38
  • To be honest @user1034749 posted to correct and proper way to handle the object creation output dir. That way you can keep the source directory clean from intermediate files. – Joel Apr 30 '16 at 23:27
  • Possible duplicate of [How do I change the directory to which CMake outputs solution and project files?](http://stackoverflow.com/questions/5399659/how-do-i-change-the-directory-to-which-cmake-outputs-solution-and-project-files) – usr1234567 May 01 '16 at 06:53
  • what's this variable "CMAKE_RUNTIME_OUTPUT_DIRECTORY" for? – user2544187 May 03 '16 at 03:09
  • See [this link](https://cmake.org/cmake/help/latest/variable/CMAKE_RUNTIME_OUTPUT_DIRECTORY.html) for an explanation of what CMAKE_RUNTIME_OUTPUT_DIRECTORY is for. You may also want to consider upvoting those answers you found useful and maybe accepting one if it answered your question. – Craig Scott Jul 03 '16 at 20:57

2 Answers2

0

You shouldn't run cmake from your source directory. It's definitely a bad practice

Indeed, that mixes your makefiles (or your Visual Studio files) with your source files and that can even corrupt your source directory (depending on what you've specified in your CMakeLists.txt).

What you have to do

First you need to create a separate build directory where you'll launch cmake.

Then, if you want to generate with MinGW makefiles, launch in the build directory the following command line:

cmake path_to_source_directory -G "MinGW Makefiles"

Further comments about your CMakeList.txt

As the command line above specifies the generator (option -G "…"), SET(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE) is useless. Take a look on this SO post and its answers. It explains why you generate Visual Studio files on your first launch of CMake and MinGW makefiles on your second launch.

Then, as @Craig Scott said, you should replace SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) by SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin). Otherwise your executables will be put in your source directory.

Community
  • 1
  • 1
cromod
  • 1,721
  • 13
  • 26
  • Regarding what you added on your edit: yes, that's where I got the solution from. I was just wondering if there was any way to SUPPRESS the unwanted output (VS files) from being produced on the first run. Also it is worth mentioning that I'm cross compiling from my computer to an embedded arm and the more configuration options I can put into a single configuration file/script would be best. Thanks for your reply nonetheless. – user2544187 May 01 '16 at 01:18
  • I don't know any automatic way with CMake to remove the unwanted outputs in your source directory. That's another reason not to build in sources. If you want to select configuration options on your generated build, you can use ccmake or CMake GUI. – cromod May 01 '16 at 07:33
  • The toolchain file is where you should put your cross-compiling options, not in your CMakeLists.txt file. That's the whole point of them. You could then have different toolchain files for different target platforms with their own set of customised compiler flags, package search paths, etc. As both my answer and @crmod's answer states, if you specify the generator on the command line rather than trying to do it in the CMakeLists.txt file, it will do what you want, i.e. not create any Visual Studio output, only MinGW. Use a script if you don't want to have to type it all the type. – Craig Scott May 01 '16 at 21:00
  • I created the toolchain file because that's what a tutorial (http://www.vtk.org/Wiki/CMake_Cross_Compiling) suggested doing. I'm new to CMake so I basically follow what tutorials and other people do. I understand that toolchain files allow you to switch between toolchains more easily I only need to use one. That's why I have it added to the CMakeList.txt file so I don't have to add the option to the command line everytime I want to build. – user2544187 May 02 '16 at 01:54
  • As you're new to SO too, I suggest you read [this topic of help center](http://stackoverflow.com/help/someone-answers). If you've another question, you can post a new one. – cromod May 02 '16 at 05:09
0

Run cmake from the directory you want to use as your build directory, not from within your source tree. That will give you an out of source build (see here for some details about this).

You have to set the CMake generator and toolchain file you want to use on the command line, you don't do it within the CMakeLists.txt file. Also, you do not include the name of the CMakeLists.txt file on your cmake command line, but rather the directory it is in. For example:

cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=/path/to/source/dir/ToolChain.cmake /path/to/source/dir

Lastly, for specifying where your executables should go, make sure you use the correct CMake variables and make sure you put them in a place within your build directory, not your source tree:

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
Community
  • 1
  • 1
Craig Scott
  • 9,238
  • 5
  • 56
  • 85