How to create a Code::Blocks project from the cmake file? - makefile

I would like to use the Code::Blocks IDE to build a C++ project based on a CMakeLists.txt file. Previously, I was using Qt Creator, where I could open a CMakeLists.txt file directly, which would create a project. I could then define the build commands, eg. cmake ../src followed by make -j8. This would run cmake on the CMakeLists.txt file, and then run make on the makefile.
How can I do something similar in Code::Blocks? If I try to create a new project, it gives me the option of creating an empty project, but then I am stuck as to where to go next...

It's similar but you have to specify the compiler, supposing that you have a folder project/build and project/src and your CMakeLists.txt is under /project. Then your commands should be something as the following :
$ cd build/
$ cmake .. -G"CodeBlocks - Unix Makefiles"
you will have your CodeBlocks project created on the /build.

Code::Blocks, has its own .cbp files which do the same things as Makefile automatically.
Therefore Code::blocks canot generate a Makefile, but you can build your project from an alredy existing Makefile, i've found a very good explaination of it on the code::blocks wiki. I do hope this will help you :)

Related

Compile & Run CLion Project from Terminal

I am trying to create generally-accessible compile & run instructions for my CLion project, but can't find the exact terminal command it uses to execute the program (it's makefile, I would assume). From the project directory in the terminal, how would I do this?
The directory looks like this:
I will add a little bit to #Stanley F.'s excellent answer.
FROM the root of the CLion project, this is what works for me. I generally run with a debug profile. The same can be reproduced for release.
When cmake loads its project, it runs
cmake -Bcmake-debug-build -H. ${CMakeOptions}
where CMakeOptions is stored in CLion at
CLion->settings->Build,Execution, Deployment->CMake->[profile]->CMake Options
My general cmake build option is
-DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
[Note the lower-case d for 'debug'. If I do not use this, my system will not work. I wish that CLion did not default to 'Debug']
So, to reproduce what CLion creates upon project reload, I run
rm -rf cmake-debug-build
cmake -Bcmake-debug-build -H. -DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
Then, to build the project, I run
cmake --build cmake-build-debug --target all
Please note that when I run the first cmake command (from CLion or the command line), cmake pulls in lots of libraries from other "precedent" projects as part of the processing of my CMakeLists.txt file. If anything in one of those precedent projects changes, I will not pull them in anew, unless I physically delete the entire cmake-build-debug/ directory. None of CLion's reset tool menu items from my experience will delete that file.
If I am running these commands from the CLion menus, then I have to physically delete the cmake-build-debug/ directory as well (if I have a change in one of the external libraries that I want to pull in).
CLion currently only supports CMake projects. So you have to invoke the CMake executable with the appropriate parameters for your project.
At first, you can delete the cmake-build-debug folder, since this is auto-generated by CLion, which itself invokes CMake. It only contains temporary files.
So your build environment basically contains the 3DTable.c, 3DTable.h and CMakeLists.txt files. At least this is what I get from the screenshot.
To build the project from command line, first navigate to the source directory. Then invoke CMake:
cd <source path of Project_1>
cmake -Bbuild -H.
cmake --build build --target all
Notes:
build is the directory, where CMake will generate temporary files and the build artifacts.
The -H. option tells CMake, where the CMakeLists.txt file is located, which in this case is the current working directory.
The library / executable for your project will be located within the build directory
CLion can tell you, you don't need to hunt.
CMake command line
Select tools\cmake\reload cmake project.
The command line is shown in the CMake window.
Build command line
Select build\build project.
The command line is shown in the messages window.
Example
Mine look like this:
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2021.2.2/bin/ninja/win/ninja.exe" -G Ninja -S C:\some_application -B C:\some_application\cmake-build-debug
...
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" --build C:\some_application\cmake-build-debug --target all -j 9
Reminder
If using Visual Studio you still to specify which environment you are using. Typically this involves using the VS command prompt or executing one of the premade scripts to set up the environment variables. See here.

running cpp project with cmake (mac)

My question is very naive. Every time I ran an algorithm, it's with an IDE so I don't know exactly what is generated in backgrown.
I try to run with my MacBook an algorithm that I didn't write and I'm blocked !
In the readme file: they said "Use cmake to generate desired projects on different platforms. (See “CM.txt” in the
“src” folder)"
In the CM.txt:
project(BlProj)
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
add_executable(BlProj
main.cpp
thi.cpp
thu.cpp
mat.cpp
Config.cpp
Stringer.cpp
)
I did: cmake CM.txt
A CM directory is generated. I will not list all the files inside: it's some cmake, bin, out, cpp, c, cxx, log, txt files.
I don't know what I can do with that!
I'm supposed to generate a compiled binary and run it with the command:
./BlProj data1 data2 10 config output
But it wasn't generated...
So I don't know how I can have the BlProj executable. Could you help me?
How do the cpp compilation work? What did cmake exactly?
Is cmake CM.txt is a good practice or not?
You can use cmake-gui to build sources.
go to CMAKE installation directory and inside bin folder run cmake-gui
Provide input where the source code is and provide an input where to build.
hit on configure and then generate.
you might need to select IDE version as well as provide additional input, it depends.
once done open generated project files with your IDE and build.

How to invoke cmake from within sublime text?

I'm using:
sublime text 3
cmake
make on Mac and mingw-make on windows
So far I'm able to compile and run hello world using cmake and make. I'd like to be able to compile, link and run from within sublime text. By default, I didn't find a build system which allows me to do this.
Also, I understand that sublime text is just an editor but it also provides the notion of a build system where one can plugin your own.
If I run:
cmake .. -G "Sublime Text 2 - MinGW Makefiles"
It generates sublime text project file. When I open this project file in sublime, it populates build system with options from the generated make file (using cmake). It builds the executable but none of the build options allows me to run it.
Can someone help me understand how can I make the sublime run the project executable ?
Please let me know if I need to add more details here.
My directory structure is like this:
root
include
linkedlist.h
src
linkedlist.cpp
CMakeLists.txt
main.cpp
My CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.9)
project(dataStructures)
#Bring the headers, into the project
include_directories(include)
#Can manually add the sources using the set command as follows:
set(MAINEXEC main.cpp)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_executable(testDS ${SOURCES} ${MAINEXEC})

Create multiple executables in XCode

There are various versions of this question but I havnt been able to find what Im looking for.
I have a bunch of cpp files in the OpenCV samples directory. I would like to create a project in XCode that can compile and build all the executables. I opened a new project, added all the sample source files, included the correct header files and the right link paths and then got stuck because I dont know how to make the project build and link executables for each relevant cpp file. Im guessing Im doing this completely wrong so any help would be appreciated.
In Xcode 4, click on the Project icon in the navigator which should bring up a Projects and Targets section in the main pane. There should be an "Add Target" button at the bottom. Just add a new target and then specify which source files you want in the "Compile Sources" section in the "Build Phases" tab.
Xcode 3 is should be similar. Add a new target and tell it which source files to use.
HTH
You don't need XCode to build samples, OpenCV on Mac OS X makes use of cmake to generate makefiles which, in turn, are being consumed by gcc.
Please read:
http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port
So, assuming you have cmake installed, in OpenCV source directory run:
sudo cmake -D BUILD_EXAMPLES=ON -G "Unix Makefiles" .
You have Makefile in your 'samples' directory now and can compile samples:
cd samples
sudo make
The sample binaries will be put in the 'bin' directory. Voilà!

CMake, Xcode and Unix Makefile

I was using classic Unix Makefile generator until I found a webpage explaining CMake could produce a nice xcode project file using "cmake -G Xcode" so I ran that command in my source directory and I get a nice xcode project. This is great but I'm facing some drawback and was wondering if there are solutions to these :
now I can't build out of tree using unix makefiles (I use to run cmake ../myproj in a build dir and then make command) this doesn't work anymore (I suppose temp files in the project dir are responsible)
some of my headers are generated by cmake (myproj.h.in became myproj.h and is include in some tests for example) but I can't find those files in the xcode project I get.
Thanks in advance for any good advice on how I can combine xcode project and unix makefile to be able to use both.
I found the answer by myself (I mean asking the good person) :
Use 2 separate build directories for each type of build files. This will ensure each build directory has the correct build files and generated files for its use.
In the add_executable() command, in addition to the source files also include the headers and generated headers for those to show up in Xcode.
You can use xcodebuild to still have a proper Xcode project and build it from the command line/shell script/Makefile, e.g.
xcodebuild -project MyProject.xcodeproj -target MyTarget -configuration Release

Resources