Getting Started with Makefile for C++(CMake or GNUMake?) - c++

I have got my first project for this semester and I have been asked to submit it with a makefile. The literature available on the internet is a bit overwhelming and combined with my laziness, I came to stackoverflow for simple answers. I have found this answer by Brendan Long as a good place to start with.
The example he gives is:
all: a3driver.o
g++ -o a3driver a3driver.o
a3driver.o: a3driver.cpp
g++ -c a3driver.cpp
which i understand. This looks exactly like the make files I have seen on a Unix system and which i used to compile c++ files(only used, did not need to understand).
Then i search further and an answer to this question suggests using CMake which is completely different from the code I have pasted above.
So my question at this stage is which direction should i take? Should I learn about the CMake or the GNUMake? I only intend to work on C++ files for now.

Only you can answer this question because it depends heavily on your needs. Cmake is a "build control file generator", not a build control program. It doesn't actually build code: instead it will create a makefile, or a Visual Studio / Xcode / Eclipse project file, etc. You then use that build program (make, Visual Studio, XCode, Eclipse) to actually build the code.
Cmake is good if you need to support all those different types of builds across all those different architectures using their native build environments. If you're happy to use make on whichever architecture you need to build on (GNU make runs on all of those as well and all those IDEs except possibly Visual Studio have good integration with native make) then using make directly is fine. GNU make has lots of advanced features which make it very flexible.
I don't really agree with esseks assessment of the autotools although I know it's a very common opinion. Also note that automake itself does not use unusual, verbose syntax: automake files are just makefiles. However they have to be processed, and autoconf is how that's done... autoconf is more obscure although not as bad as people make it out to be, depending on your needs. This isn't the place for that discussion however.
I personally find cmake format even more annoying and strange than autotools, and it doesn't meet my needs in many ways (for example it's support for cross-compilation is not as good as autotools'). However I have to say its ability to generate native project files is really excellent--if you need it.

If you need a really really dead simple makefile for compiling one or few files only, then you are done with:
compile:
g++ myprogram.cpp -o myprogram
(note that lines must be indented with tab, not spaces).
If you need flexibility, you are on the right path with CMake. I suggest you to explore CMake, starting from their good tutorial or a simple example -- as the basics are simpler to undestand from code rather than learn from manual.
My personal opionion is to avoid GNU Automake (colloquially known as Autohell) because of the unusual, verbose syntax that sometimes scares beginners and tricks more experienced users.
EDIT: CMake is not used to compile, rather, it can generate makefiles for you, starting from a synthetic description of the project (where are the files to be compiled? What libraries are required? etc.). And it does this by checking for libraries, identifying compiler and carrying out other sanity check you would need to code by yourself otherwise.

Related

How to compile a visual studio c++ project on linux efficiently

I'm having a C++ Project in Visual Studio 2017 which uses curl. It compiles fine on a linux machine. But i think my procedure is not efficient. Im just copying my source files to the linux machine and run
g++ one.cpp two.cpp etc.cpp -lcurl
or even
g++ *.cpp -lcurl -o output
Is there something to do this a "cool" way? Like cmake or something (sorry im not into this). An Example would be really helpful
Your options range wildly from:
Just keep doing what you're doing
Wrap that command in a shell script so you don't have to keep typing it
Make a Makefile to auto-generate that command
Use CMake to auto-generate the Makefile (or automake or something else)
Use an IDE (examples for various platforms: Eclipse, Xcode, Visual Studio) to fully manage the project for you, including build rules/commands — Visual Studio Code in particular may be of interest here
It is completely up to you what you pick.
Personally, in your situation, right now I'd just throw together a Makefile and be done with it, until your needs become more complex.
Whichever of the latter 3 options you pick, there are abundant examples online and in your book already.
I would say, please don't use a really complicated solution to solve a simple problem just because it's "cool", because that's not what cool is.

Better way to give provide path of libraries while compiling in C++

I pretty new to C++. I was wondering, as what is considered generally a neat way to provide paths for various files/libraries while compiling or executing c++ codes.
For ex:
I have Boost libraries installed in some location on my system. Lets call it X
In order to execute anything I have to type in
c++ -I LongpathWhichisX/to/boost_1_60_0 example.cpp -o example
Similarly, also Long path for the input file while executing the code.
Is there a better way to address it. Is it possible to create environment variables lets Y, which refers to path 'X'. And we can use following command to compile code
c++ -I Y/to/boost_1_60_0 example.cpp -o example
Best way is to use build tools. For example you can use Make. You define all your include paths (and other options) in the Makefile. In console you just have to call make to build your project or something like make run to run your project.
The usual way is to make a Makefile where you can specify all needed paths and compile options in proper variables.
If you don't want/need a Makefile and rather want to run compiler from command-line, then you may use the CPATH environment variable to specify a colon-separated list of paths to include files.
This is a broad question but the other answers highlight the most important step. It is essential to learn build tools like make because they will make it easier to build your projects during development and for others to build it later. In the modern programming age though this is not enough. If you are using something like Boost (which targets many platforms) you will probably want to make your build cross-platform as well. For this you would use either cmake or autotools which both have scripts that make it much easier to locate the Boost libraries (and others).
Any other build systems, in my opinion, are a pain and are the bane of maintainers of Linux distributions. CMake used to be in that catergory but it has gained wide acceptance now. CMake targets building cross-platform projects across operating systems (Windows and Unixes) better (again in my opinion) because it attempts to provide the native build system on each platform (for example: Visual Studio in Windows, Make on all Unices, XCode on Mac). The autotools instead target the Unix environment with much greater depth (you have a bit of a harder time on Windows, but you can target embedded Unix systems to high end Unix server systems with much more flexibility).
Note: Autotools support for cross-compiling is superior in almost every way to other solutions. I always cringe when I download something that needs to be cross compiled for Arm Linux and it uses some weird build system. Funnily enough, boost is one of these.
This is a bit of a long winded answer. In summary, it is essential that you learn a build system for native development. It is part of your skill set and until you have that skill you can't really contribute to open-source projects or even your employer developing closed-source projects.

How to export a C program that compiles from command line in Linux to an IDE?

There are a lot of open source C/C++ projects, most of them can be compiled using "make" in Linux. Is there any easy way to export this to an IDE, for example Microsoft Visual C++, CodeBlock, or Eclipse?
Even if it is an involved work, is there any step by step help for doing that?
Thanks for the help.
TJ
Considering that most (if not all) of the IDEs support importing a makefile or cmake based project, that should be a no-brainer.
Be careful about configure scripts though, as they generally do important stuff not supported automatically by an IDE.
In fact, build systems like cmake, premake and scons were invented because there was no standard way for an IDE to know what a script is doing. (That, while sacrificing flexibility, makes the system standard.)

C and C++ programming on Ubuntu 11.10 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've recently installed Ubuntu 11.10 and along with it the CodeBlocks IDE and I am aware that I have gcc and the std libraries by default.
My questions are:
Do you you have any tips for a new C++ programmer on Ubuntu?
Any libraries I should get from the start?
A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)
Any programming boons or traps I should be aware of from the start?
You don't need an IDE to code in C or C++ on Ubuntu. You can use a good editor (like emacs, which you can configure to suit your needs.).
Some few tips for a newbie:
Always compile with -Wall -Wextra and perhaps even with -Werror -pedantic-errors
Order of arguments to the compiler (gcc or g++) are really important; I recommend:
general warnings and optimization flags (e.g. -Wall, -g to get debug info, -O, -flto etc, or -c to avoid linking , ...)
preprocessor options like -I include-dir and -D defined-symbol (or -H to understand which headers get included) etc..
source file[s] to compile like hello.c or world.cc
if you want to link existing object files else.o, add them after the source files
linker options (if relevant), notably -L library-dir (and probably -rdynamic if your program uses plugins with dlopen(3) ....)
libraries (like -lfoo -lbar from higher-level libraries like libfoo.so to lower-level libraries.
output file (i.e. produced executable), e.g. -o yourexec.
Always correct your source code till you got no warning at all. Trust the compiler's warnings and error messages.
Learn how to use make and to write simple Makefile-s; see this example.
there are other builders, e.g. http://omake.metaprl.org/ etc
Compile your code with the -g flag to have the compiler produce debugging information; only when you have debugged your program, ask the compiler to optimize (e.g. with -O1 or -O2), especially before benchmarking.
Learn how to use gdb
Use a version control system like svn or git (even for a homework assignment). In 2015 I recommend git over svn
Backup your work.
Learn to use valgrind to hunt memory leaks.
NB
The advices above are not specific to Ubuntu 11.10, they could apply to other Linux distributions and other Ubuntu versions.
QT Creator is a good IDE, that works well also with simple Makefile based projects. Also, as a C++ programmer you should check out Dia and Dia2Code for automatic generation of stubs from UML diagrams.
Since you ask more than one question I will answer each separately.
Do you you have any tips for a new C++ programmer on Ubuntu?
Learn some build system such as CMake or SCons. Although understanding how make and Makefiles work is useful there is a tendency of moving away from make to more high-level tools which also provide configure-like functionality. Make is often used for command-line build, for example with CMake you can generate Makefiles and build your projects using make.
Use a version control system such as git or Mercurial. I also recommend keeping those your projects you care about on some external service like github at least for the purposes of backup.
Pay attention to compiler warnings but keep in mind that warnings only catch a fraction of possible errors. A more complete picture can be obtained using static analysis tools and dynamic analysis tools like Valgrind.
Any libraries I should get from the start?
You've already got the main one which is called the C++ Standard Library. Make sure that you know what it provides.
Boost will cover most of the remaining needs except GUI.
Gtkmm and Qt are two major C++ GUI frameworks.
A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)
Eclipse - for a long time I've been thinking of it as a Java only IDE, but in fact it is an excellent IDE for almost anything (I even wrote my PhD thesis in it using TeXlipse plugin) and C/C++ support is improving all the time. Also CMake can generate Eclipse CDT project files.
Qt Creator - another excellent C++ IDE. It is very fast and has native CMake support
Any programming boons or traps I should be aware of from the start?
From my experience the most common sources of errors in C++ are pointers and resource management in case of exceptions. Make sure you understand and use the RAII idiom and smart pointers.
For a more complete list of traps and recommendations see the answers to this question.
Some tips besides those which are already mentioned:
Valgrind is your friend in finding memory leaks. You may also use valgrind --tool=callgrind and KCacheGrind to see where does your program spend time on execution.
If you are going to distribute your program, you should learn autotools or cmake. The first is a classical tool, a bit bloated, the second is more modern.
Geany is a nice IDE if you are looking for something lightweight. Otherwise, take a look at Code::Blocks, Eclipse/CDT and NetBeans.
Since I am not sure what you meant by "std libraries", I should mention that besides standard C library, there are a lot of POSIX functions and interfaces, which are common to most *nix-systems, including Mac OS X.
Eclipse/CDT runs really well on Ubuntu.
Boost provide a whole bunch of libraries that are commonly used and can come in handy. Anyway, I'm not really sure this questions fits in too well on a Q&A board.
EDIT: As suggested by Basile, Makefiles and learning to use gdb are great ideas. There are plenty of neat flags to use with gcc also, for helping to debug your code, optimize it, produce assembly instructions, etc.
On the first steps of programming you should not use IDE because you will better understand what happens backside :) GCC or G++ and stdlib will be sufficient. You also should read about Makefiles, SVN(CVS, GIT), Autotools or CMake to manage your projects. If you want make GUI applications you should learn GTK+ or Qt. If you want real IDE for your needs try Eclipse with C/C++ plugins. Good luck :)
If you are familiar with the command line you can use an editor like vim and gcc/g++ to compile your code, learning make svn git is also recommend.
In case you are not familiar with the command line or you prefer using the UI :NetBeans is also a good IDE you can use to develop in c/c++ and java.
To install netbeans: open firefox and point to apt://netbeans
I hope this will help you.
I think Netbeans is good. Same UI in Microsoft Windows and Linux. Built-in version Controller and installed Git by default.
No extra library added (as oposit of QT)
Library: I recommend you to use Boost. You can find many libraries in it.
IDE: Eclipse and QTCreator are good IDEs, but I think it is also very important to use text editor + makefile. Vim, Emacs or Sublime Text is good choice.
Always remember to backup your code.

How to build a boost dependent project using regular makefiles?

I'm working on a c++ project, and we recently needed to include a small part of boost in it. The boost part is really minimal (Boost::Python), thus, using bjam to build everything looks like an overkill (besides, everyone working on the project feels comfortable with make, and has no knowloedge of jam).
I made quite some tests already, but I cant find a way to include the formerly mentioned library in my makefile and make the build succesful.
All your help is deeply apreciated. :)
You can use Boost's bcp utility to extract only the subset of Boost you need.
That will minimize your build time & size.
That doesn't answer your question, though. You may want to consider building the Boost.Python libraries separately and checking them directly into your source control system. Then nobody would need to build them.
I had the same problem and found a solution in this tutorial. You 1) need to compile the source into an object file with the -fPIC gcc option, and 2) compile this object into a library with the -shared gcc option. Of course you have also to link against the Boost.Python library (generally -lboost_python, however for my debian system it is for example -lboost_python-mt-py25, I have also to add -I/usr/include/pythyon25). In my makefile I end up doing those two steps in one command. See also p. 13 of this presentation.
If you're uncomfortable with bjam, you might want to consider using Boost.Cmake.
Alternatively you should at least be able to see more easily what they are doing then with the bjam files.
Run bjam from the makefile, just for building that part