HomeЛюди и блогиRelated VideosMore From: devlogs

Compiling C++ using CMake and Make (CS20170204)

452 ratings | 44559 views
This dev log shows how to compile C++ code using a Makefile generated by CMake. This will also allow you add a flag to compile c++14 compliant syntax. Requirements: * g++ * cmake 3.x * text editor
Html code for embedding videos on your blog
Text Comments (39)
Utsav Chatterjee (7 days ago)
Can you create a video showing how to link libraries in cmake 3 and above. For example SFML.
Agustin Albiero (1 month ago)
I've a question, why are you using linux with c++? I know that a compiled c++ file is an .exe that can only be used in windows...
chinmay hundekari (1 month ago)
In your case, exe is created because you use windows environment and the output target is the Windows desktop environment. In Linux, it won’t generate a exe. Also, You can use compilers to target other environments, so you can ask compilers to compile for other Operating systems.
Rafael Nogueira (4 months ago)
Finally a comprehensive tutorial ! But what I do if I want to make custom folders like : build ( with executable inside ) and src ( with sources ). ?
Space Time (5 months ago)
Excellent, splendid tutorial, thank you bro
devlogs (5 months ago)
Thank you. Really swamped with work but will hopefully make more soon.
commenter (5 months ago)
Probably I look like an idiot, but is there any way to get .cpp .h or .rc files from CMake converted project? There is a lot of interesting stuff on GitHub converted with CMake. But I really hate this converter.
May Habuoi (5 months ago)
This is super helpful. In case anyone missing cmake. You have to install it before hand to use it :) https://ask.fedoraproject.org/en/question/31289/how-to-install-cmake/
Miłosz (5 months ago)
Are you from England?
devlogs (5 months ago)
Nope. Sorry.
Alessandra Scutti (6 months ago)
the best tutorial ever!!
devlogs (6 months ago)
Thank you :)
ViralTaco (7 months ago)
What is it with people and `return 0;` in main? The closing bracket is the same as `return 0;` This is why you don't get an error when you omit it. It's useless.
GetCracked (14 days ago)
good coding style lmao
Lars Jul Vistisen (7 months ago)
Hey man, great tutorial, it helped me a lot :-)
devlogs (7 months ago)
Thank you. :) Glad it was able to.
Azen Kwed (8 months ago)
std::cout << "Hello, World!\n"; don't use std::endl if you don't need to flush.
Josh Torres (10 days ago)
+Rafael Nogueira I know right? This is a CMAKE TUTORIAL not a coding tutorial.
Rafael Nogueira (4 months ago)
lol , why you need to comment this ? Programmers from around the internet are so funny.... Maybe it's too much bullying in the childhood age...
Ruki Wang (9 months ago)
you can try xmake (http://xmake.io)
bo100nka (10 months ago)
what map setting in vim should i use to dynamically determine the "project dir" of a c++ and then run :!make command so regardless of what is my working directory it will always use the correct one? i dont want to hardcode project path to key bindings.. what if i switch other project?
Alpero (10 months ago)
Hello I have a project in C++ but I was given a zip file that has .depend.Makefile.Array .DS_Store Array source file Array heater file Array.mpc driver source file And they already have the necessary function prototype in them all I have to do is to write function definition so that it can complile but I was asked to install perl to do them.My problem I have never used perl before and even never heard of files called (.depend.Makefile.Array .DS_Store, .DS_Store, Array.mpc) any Do you have any idea what I should here?
Holdi Poldi (10 months ago)
quickly but good ! I like it.
עידו נסים (11 months ago)
thank you!
Florian Wolters (1 year ago)
"Get your hands off CMAKE_CXX_FLAGS!" (citation from the "Effective CMake" talk available at https://www.youtube.com/watch?v=bsXLMQ6WgIk&t=1s). Either use the CXX_STANDARD property (https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD) on a target or use the target_compile_features command (https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#requiring-language-standards). Also do not specify CMAKE_BUILD_TYPE as a variable in the CMakeLists.txt file. Instead pass it as an option to the cmake executable (-DCMAKE_BUILD_TYPE=Debug).
Muntasir (1 year ago)
This video is a lifesaver, thank you so much!
hunar omer (1 year ago)
1:36 what button you pressed to get there .. i didnt know that was a thing XO
bo100nka (10 months ago)
It is "NERDTree" plugin for VIM that you must install. Then either calling :NERDTree as a command manually or mapping it to a key of your choice in your .vimrc file
Hey man, great tutorial, thanks for the upload. Will you be doing any in-depth cmake tutorials?
devlogs (6 months ago)
If I have time I will. :)
Axe Leo (1 year ago)
hi mate. good video, very clear and simple. but lets say you have hundreds of .cpp files. can u avoid listing all of them in the cmakelist file. if yes, how?
Naxaes (5 months ago)
I think the proper solution to such problem is to not have hundred of .cpp files in one directory. Instead, divide it up into several subdirectories, based on their usage. Then add a CMakeLists.txt file in each subdirectory and try to make them as modular as possible. If you've divided them properly, I don't think it's often you add or remove files, and if you do it's trivial since each CMakeLists.txt file only needs to handle few .cpp files.
Florian Wolters (1 year ago)
Correct, this helps if following a strict workflow. But also note that this approach is not recommended by CMake. I am refering to the official CMake documentation (https://cmake.org/cmake/help/latest/command/file.html): "We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate."
Meak (1 year ago)
create a variable "sources_files" containing the list of files : file(GLOB sources_files *.cpp) and use it in add_executable(sample ${sources_files})
Zongyao Jin (1 year ago)
Hi! What is the Vim plugin you use for displaying your editor with the directory structure? Thanks
Zongyao Jin (1 year ago)
Awesome, looking forward to your new videos!
devlogs (1 year ago)
Yep. Got some requests for that. Will do one with my extra time. :) Thanks for watching. Let me know if you have any other questions. Will try to answer as best I can.
Zongyao Jin (1 year ago)
Thank you very much for the reply and the awesome video. I'm new to Vim and I found it very powerful. And your setup makes it even more magical, I really appreciate that. Would you possibly please upload a short video tutorial about how to setup Vim and whatever other softwares (tmux or screen?) like you did there to make it working like an IDE? That would be absolutely super helpful!
devlogs (1 year ago)
Hi Zongyao. I'm using several plugins for my workflow. For the directory structure, I'm using nerd tree. Please refer to this gist for my complete setup: https://gist.github.com/ralampay/bfe5787f600bc691e328f00fc5aa9aeb

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.