I try to use the c++ language bindings for the ev3dev lego brick: https://github.com/ddemidov/ev3dev-lang-cpp
The instruction is as follows:
mkdir build
cd build
cmake .. -DEV3DEV_PLATFORM=EV3
make
I am running windows and have cmake and mingw available. After running cmake it creates some files in the build directory. However: There is no makefile which could be picked of by make. So I am wondering how iam supposed to compile these bindings
On Windows, CMake generates a MSVC solution by default. Check for a .sln file in your build directory.
The instructions you linked are assuming a Unix-ish platform, where the default is to create Makefiles.
If you actually want Makefiles on Windows, add -G "Unix Makefiles" to the cmake line.
If you want to use MSVC as compiler but work on the command line, another option is -G "NMake Makefiles", and calling nmake after that.
Make sure to delete your build directory before trying to build a new generator target. CMake can be touchy about that.
Check cmake --help for a list of available options. (Especially the generator targets are platform-specific.)
Related
I would like to convert my project from a Visual Studio solution to build with CMake and compile it with Makefiles.
This is a 2-part question.
Right now the CMakeLists.txt is:
cmake_minimum_required(VERSION 3.13.0)
project(Project2015 CXX)
add_executable(Project Source/main.cpp)
When I run cmake .. out of the build directory, it generates *.vcxproj and *.sln files, but there is no Makefile. How can I change the CMakeLists file to generate a Makefile?
What is the command line equivalent compiler to gcc for windows? And how do I set this compiler as the target for CMake and the generated Makefile?
Reading about the build tools https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019
Do I need to target the cl.exe compiler? Would this work with CMake and Makefiles?
I'm reading online that these command line flags will set the compiler, how can I add these to the CMakeLists.txt to be used automatically?
DCMAKE_C_COMPILER=cl
DCMAKE_C_COMPILER_FORCED=ON
DCMAKE_CXX_COMPILER=cl
DCMAKE_CXX_COMPILER_FORCED=ON
DCMAKE_BUILD_TYPE=Debug
DCMAKE_INSTALL_PREFIX=%CFITSIO_DIR%
G"NMake Makefiles"
You should use the build tool mode of CMake for builds from the command line.
After configuring your project for a 64bit build using Visual Studio 2019 e.g. with
cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A x64
you would run
cmake --build <builddir> --target ALL_BUILD --config Release
For further options see here for an almost quiet build from the command line see here.
As suggested by #vre, you can run everything from the command line, while still using the Visual Studio generator. Just use CMake's command line build tools:
cmake ..
cmake --build . --config Release
This way, you don't have to open Visual Studio at all to build your libraries/executables.
Another option is to use Microsoft's nmake utility, which will generate NMake Makefiles. You can tell CMake to use this generator instead using this:
cmake -G"NMake Makefiles" ..
The full list of CMake generators you can choose from is listed here.
If you don't want to manually set the CMake generator in the command line, you can set it at the top of your CMakeLists.txt file:
set (CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE)
It will be used on the second CMake configuration in this case, as the first run will use the system default generator. If you want CMake to use it on the first configuration, you can utilize the Preload.cmake procedure outlined in this answer.
1. cmake is a command from CMake software: preparation for build automation system; make and make install are commands from Make software: build automation system.
2. From reading this post, what I understand is that:
a. This "cmake and make" stuffs actually use g++ / gcc in its implementation. cmake and make stuffs are basically just tools in using g++ / gcc. Is that correct?
b. gcc / g++ are the compiler that do the actual work.
c. So I can just use gcc / g++ directly without using the make and CMake things?
3. According to this stackoverflow answer: CMake takes a CMakeList.txt file, and outputs it to a platform-specific build format, e.g., a Makefile, Visual Studio, etc.
However when I came across this openCV installation :
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
It executes cmake command in a directory where there is no CMakeLists.txt file. Can you explain and elaborate on this?
4. The usual steps that I've seen are: cmake, make, sudo make install.
I read this stackoverflow post, what I understand:
(i) make is for building the project.
(ii) make install is to copy the binary / executables to the installed directories.
a. So when we make, where are the result / binary files / executables stored at?
b. If we only run make without make install, does it mean that the files are not generated?
c. I came across this openCV tutorial on using openCV with GCC and CMake. It uses:
cd <DisplayImage_directory>
cmake .
make
Why doesn't it do make install as well?
5. In summary:
CMake takes CMakeList.txt file (which is cross platform) to generate a Makefile (which is specific to a platform).
I can just write Makefile manually and skip the CMake step. but it is better to do with the CMake step because it is cross platform, otherwise I have to rewrite the Makefile again if I change platform.
Make takes Makefile (which is generated by CMake or written manually) as a guide to compile and build. Make basically uses gcc / g++ or other compiler in its work. Make itself is just a tool for the compiler.
Make install put the result / executables into the install path
CMake generates files for other build systems. These can be Makefiles, Ninja files or projects files for IDEs like Visual Studio or Eclipse. The build files contain calls to compilers like GCC, Clang, or cl.exe. If you have several compilers installed, you can choose one.
All three parts are independent. The compiler, the build system and CMake.
It is easier to understand when you have the history. People used their compiler. Over time they added so many flags, that it was cumbersome to type them every time. So they put the calls in a script. From that the build systems (Make, Ninja) evolved.
The people wanted to support multiple platforms, compilers, scenarios and so on and the build system files became hard to maintain and their use was error-prone. That's the reason people invented meta build system that creates the files for the actual build system. Examples are Autotools or CMake.
Yes
CMake does not use your compiler, make does not implement it, but it calls (uses) the compiler.
The CMakeLists.txt file should be in the parent directory of release. The last argument of the CMake call indicates the path where the CMakeLists.txt file is located.
Right, make generates the file in the build directory. In your example from 3. release is the build directory. You can find all the generated files and use them. Installing is optional, especially if you want to develop the software, you are not installing it.
Try writing Makefiles for a large project and you will see how much work it is. But yes, everything in 5 is right.
Is there a way to configure CLion to use a local makefile to compile code, rather than CMake? I can't seem to find the way to do it from the build options.
Update: If you are using CLion 2020.2, then it already supports Makefiles. If you are using an older version, read on.
Even though currently only CMake is supported, you can instruct CMake to call make with your custom Makefile. Edit your CMakeLists.txt adding one of these two commands:
add_custom_target
add_custom_command
When you tell CLion to run your program, it will try to find an executable with the same name of the target in the directory pointed by PROJECT_BINARY_DIR. So as long as your make generates the file where CLion expects, there will be no problem.
Here is a working example:
Tell CLion to pass its $(PROJECT_BINARY_DIR) to make
This is the sample CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.4)
project(mytest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(mytest COMMAND make -C ${mytest_SOURCE_DIR}
CLION_EXE_DIR=${PROJECT_BINARY_DIR})
Tell make to generate the executable in CLion's directory
This is the sample Makefile:
all:
echo Compiling $(CLION_EXE_DIR)/$# ...
g++ mytest.cpp -o $(CLION_EXE_DIR)/mytest
That is all, you may also want to change your program's working directory so it executes as it is when you run make from inside your directory. For this edit: Run -> Edit Configurations ... -> mytest -> Working directory
While this is one of the most voted feature requests, there is one plugin available, by Victor Kropp, that adds support to makefiles:
Makefile support plugin for IntelliJ IDEA
Install
You can install directly from the official repository:
Settings > Plugins > search for makefile > Search in repositories > Install > Restart
Use
There are at least three different ways to run:
Right click on a makefile and select Run
Have the makefile open in the editor, put the cursor over one target (anywhere on the line), hit alt + enter, then select make target
Hit ctrl/cmd + shift + F10 on a target (although this one didn't work for me on a mac).
It opens a pane named Run target with the output.
Newest version has better support literally for any generated Makefiles, through the compiledb
Three steps:
install compiledb
pip install compiledb
run a dry make
compiledb -n make
(do the autogen, configure if needed)
there will be a compile_commands.json file generated
open the project and you will see CLion will load info from the json file.
If you your CLion still try to find CMakeLists.txt and cannot read compile_commands.json, try to remove the entire folder, re-download the source files, and redo step 1,2,3
Orignal post: Working with Makefiles in CLion using Compilation DB
To totally avoid using CMAKE, you can simply:
Build your project as you normally with Make through the terminal.
Change your CLion configurations, go to (in top bar) :
Run -> Edit Configurations -> yourProjectFolder
Change the Executable to the one generated with Make
Change the Working directory to the folder holding your executable (if needed)
Remove the Build task in the Before launch:Activate tool window box
And you're all set! You can now use the debug button after your manual build.
Currently, only CMake is supported by CLion. Others build systems will be added in the future, but currently, you can only use CMake.
An importer tool has been implemented to help you to use CMake.
Edit:
Source : http://blog.jetbrains.com/clion/2014/09/clion-answers-frequently-asked-questions/
I am not very familiar with CMake and could not use Mondkin's solution directly.
Here is what I came up with in my CMakeLists.txt using the latest version of CLion (1.2.4) and MinGW on Windows (I guess you will just need to replace all:
g++ mytest.cpp -o bin/mytest by make if you are not using the same setup):
cmake_minimum_required(VERSION 3.3)
project(mytest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_custom_target(mytest ALL COMMAND mingw32-make WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
And the custom Makefile is like this (it is located at the root of my project and generates the executable in a bin directory):
all:
g++ mytest.cpp -o bin/mytest
I am able to build the executable and errors in the log window are clickable.
Hints in the IDE are quite limited through, which is a big limitation compared to pure CMake projects...
After I generated Makefile with CMake I ran in MSYS:
msys>make
It launched cmd.exe within msys and started new make.exe process that did nothing.
It's not a single case - every project that I try to build with CMake leads to this trap.
What's wrong?
MSYS and MinGW makefiles (as CMake understands it) is not the same. In this case I used to generate MSYS makefiles.
If you want to use MinGW compiler, I suggest you use MinGW shell prompt instead of MS cmd.exe. This ensure that several MinGW compiler specific environment variables are correctly set.
Then use "MinGW Makefiles" as generator and mingw32-make as make program.
If you use MSYS you must use "MSYS Makefiles" and the make program should be called simply make (if you have installed it correctly).
If you generate "MinGW Makefiles" and call make, the wrong make program is called. Specify explicitely mingw32-make.
I'm trying to build the LLVM OCaml bindings under MinGW on Windows using CMake. I've tweaked a little bit with the LLVMBuild.txt in the bindings directory without luck. How do I build them?
Edit: To answer ygrek's question:
First attempt:
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ..
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make
I realized that llvm/bindings/ocaml wasn't being built. I then connected the LLVMBuild.txt files in many directories with the CMake build system. llvm/bindings/LLVMBuild.txt didn't contain any instructions to build subdirectories so I added them:
[common]
subdirectories = ocaml
llvm/bindings/ocaml/LLVMBuild.txt didn't have an LLVMBuild.txt either so I added one:
[component_0]
type = Group
name = OCamlBindings
parent = Bindings
I tried building this, but the OCaml binding weren't built. I think it's because there are no LLVMBuild.txt in any of the ocaml subdirectories. I had considered adding LLVMBuild.txt files to all of the ocaml subdirectories but realized I didn't know what their dependencies were or how to specify them.
I didn't want to pursue this aimlessly if it isn't possible or if someone had already done the work. It seems like there's a disconnect between the CMake build system & the bindings directories.
Not a real answer, but still.
The note at the beginning of llvm/bindings/Makefile.ocaml reading
An ocaml library is a unique project type in the context of LLVM, so rules are here rather than in Makefile.rules.
seems to suggest that building ocaml bindings with cmake is not supported..