Making a Makefile - c++

How I can make a Makefile, because it's the best way when you distribute a program by source code. Remember that this is for a C++ program and I'm starting in the C development world. But is it possible to make a Makefile for my Python programs?

From your question it sounds like a tutorial or an overview of what Makefiles actually do might benefit you.
A good places to start is the GNU Make documentation.
It includes the following overview "The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them."
And its first three chapters covers:
Overview of make
An Introduction to Makefiles
Writing Makefiles

I use Makefiles for some Python projects, but this is highly dubious... I do things like:
SITE_ROOT=/var/www/apache/...
site_dist:
cp -a assets/css build/$(SITE_ROOT)/css
cp -a src/public/*.py build/$(SITE_ROOT)
and so on. Makefile are nothing but batch execution systems (and fairly complex ones at that). You can use your normal Python tools (to generate .pyc and others) the same way you would use GCC.
PY_COMPILE_TOOL=pycompiler
all: myfile.pyc
cp myfile.pyc /usr/share/python/...wherever
myfile.pyc: <deps>
$(PY_COMPILE_TOOL) myfile.py
Then
$ make all
And so on. Just treat your operations like any other. Your pycompiler might be something simple like:
#!/usr/bin/python
import py_compile
py_compile.compile(file_var)
or some variation on
$ python -mcompileall .
It is all the same. Makefiles are nothing special, just automated executions and the ability to check if files need updating.

How i can make a MakeFile, because it's the best way when you distribuite a program by source code
It's not. For example, KDE uses CMake, and Wesnoth uses SCons. I would suggest one of these systems instead, they are easier and more powerful than make. CMake can generate makefiles. :-)

A simple Makefile usually consists of a set of targets, its dependencies, and the actions performed by each target:
all: output.out
output.out: dependency.o dependency2.o
ld -o output.out dependency.o dependency2.o
dependency.o: dependency.c
gcc -o dependency.o dependency.c
dependency2.o: dependency2.c
gcc -o dependency2.o dependency2.c
The target all (which is the first in the example) and tries to build its dependencies in case they don't exist or are not up to date. will be run when no target argument is specified in the make command.

For Python programs, they're usually distributed with a setup.py script which uses distutils in order to build the software. distutils has extensive documentation which should be a good starting point.

If you are asking about a portable form of creating Makefiles you can try to look at http://www.cmake.org/cmake/project/about.html

Related

What do you mean by 'make' command in linux?

First, i know that make is used for building the code. But which code?
But what does it mean by building a code, and after executing the make command, what is presented to the user?
Second, how is it different from make build_for_e2e?
What Wikipedia tells about make
Make is a build automation tool that automatically builds executable programs and libraries from source code
Compilation process becomes big and complex in big projects, where numbers of files need to be compiled, with flags and libraries. Where it will become hard for people to compile it one by one. So these types of tools were introduced, there are more similar tools available for same use like cmake, gradle, maven. e2e's Build is also a form of build process, with different form of specifications.
For C people mostly use make. It is helpful for porting software packages in different systems.
How make is used:
As said make is a tool, which will be available in our system, we can execute it by giving command make in the directory which needs to be compiled. Then make looks for Makefile, which is provided in the package directory and it contains information about compilation of the project. Then make as per info gathered from Makefile, it compiles the package.
You can also create Makefile for your project, so that it can be also supported and compiled with make. Simple tutorial for it can be found here. For big projects you can use gnu autotools contains autoconf and automake which will help you to create your all files required by make automatically. You can find tutorial regarding it here and here . These contains some basic information, you can find some advance tutorial regarding autotools, use google for more information on it.

Is it possible use Go build with extra build steps?

What to do when go build is not enough and ones need to run extra commands along with go build? Does go tools have this use case covered? If so, what's the convention?
I noticed it's possible to pass extra flags to build tools with:
//#cgo pkg-config: glib-2.0 gobject-2.0 etc etc
import "C"
Is it possible to run extra commands or at least tell go build to use a Makefile?
No. The go tool isn't intended to be a generic build system. There are some provisions made for cgo (like pkg-config), but it's not extendable.
in go1.4 there will be the generate command. This will let you run arbitrary commands for pre-processing source files, but it always has to be a separate step that is run explicitly. You can't hook it into go get, go build, or go install.
Many projects that require a more complicated build use a script or a Makefile, and eschew the general ability go get. Library packages however should strive to be get-able for simplicity in dependecy resolution.
I don't believe you can add extra steps.
pkg-config is a special keyword for the built in build system.
Normally, more complex builds are accomplished with a makefile that calls go build at the appropriate step.

Getting a library to work (QuadProg++ )

I'm trying to use the Quadprog++ library (http://quadprog.sourceforge.net/). I don't understand the instructions though.
To build the library simply go through the ./configure; make; make
install cycle.
In order to use it, you will be required to include in your code file
the "Array.hh" header, which contains a handy C++ implementation of
Vector and Matrices.
There are some "configure", and "MakeFile" files, but they have no extension and I have no idea what to do with them. There are also some ".am", ".in" and ".ac" extensions in the folder.
Does this look familiar to anyone? What do I do with this?
(Edit: On Windows.)
This package is built using the autotools. These files you talk to (*.am, *.in...) are because of the tools automake, and autoconf.
Autotools is a de-facto standard in the GNU/Linux world. Not everybody uses it, but if they do you ease the work of package and distribution managers. Actually they should be portable to any POSIX system.
That said, I'm guessing that you are using a non-unix machine, such as Windows, so the configure script is not directly runable in your system. If you insist in keep using Windows, wich you probably will, your options are:
Use MinGW and MSYS to get a minimal build enviroment compatible with autotools.
Use Cygwin and create a POSIX like environment in your Windows.
Create a VS project, add all the source of the library in there, compile and debug the errors they may arise, as if the code had been written by you.
Search for someone that already did the work and distributes a binary DLL, or similar.
(My favourite!) Get a Linux machine, install a cross-compiler environment to build Windows binaries, and do configure --host i686-mingw32 ; make.
This instruction say how can be build an program delivered like a tarball in Linux. To understand take a look on Why always ./configure; make; make install; as 3 separate steps?.
This can be confusing at first, but here you go. Type these in as shown below:
cd <the_directory_with_the_configure_file>
./configure
At this point, a bunch of stuff will roll past on the screen. This is Autoconf running (for more details, see http://www.edwardrosten.com/code/autoconf/index.html)
When it's done, type:
make
This initiates the build process. (To learn more about GNU make, check out Comprehensive gnu make / gcc tutorial). This will cause several build messages to be printed out.
When this is done, type:
sudo make install
You will be asked for the root password. If this is not your own machine (or you do not have superuser access), then contact the person who administers this computer.
If this is your computer, type in the root password and the library should install in /usr/local/lib/ or something similar (watch the screen closely to see where it puts the .so file).
The rest of it (include the .hh file) seems self-explanatory.
Hope that helps!

Figuring out the intended target system of a makefile

I am trying to understand the exact actions being taken by a make file (first time working with builds using make).
I want to know whether the make file is intended to be used by BSD make, GNU make or Windows nmake. 1) How can I do this without reading documentation of all three and understanding their differences?
2) After reading several articles, I have come to the conclusion that make is a utility for the primary purpose of building source code into an executable or a DLL or some form of output, something that IDEs usually allow us to do. Makefile is the means of giving instructions to the make utility. Is this right?
3) What is the relation between make and the platform for which the code will be built?
4) The commands that are issued under a dependency line (target:components) are shell commands?
Thanks in advance.
How can I do this without reading documentation of all three and understanding their differences?
Well, most likely, that you will use GNU Make. I believe, it is relatively simple to distinguish Makefiles written for different versions of Make.
AFAIK, GNU Make and BSD Make have many differences, at least in their language syntax. For example, in GNU Make a typical conditional directive looks like:
ifdef VARIABLE
# ...
endif
And in BSD Make it is something like this (though I'm not sure):
.if
# ...
.endif
See also:
How similar/different are gnu make, microsoft nmake and posix standard make?
Use the same makefile for make (Linux) and nmake(Windows)
BSD Make and GNU Make compatible makefile
Makefile is the means of giving instructions to the make utility. Is this right?
Yep, absolutely right.
What is the relation between make and the platform for which the code will be built?
Make is just an utility for tracking dependencies between artifacts to be built and running commands in the proper order to satisfy these dependencies. Which exact commands should be used to accomplish the task is up to the user. Thus you can use Make for cross compilation as well.
The commands that are issued under a dependency line (target : components) are shell commands?
Yes, typically Make spawns a sub-shell which executes the specified recipe.

C++ development on linux - where do I start?

I decided to leave my windows install behind and am now running Debian as my default OS. I have always coded in Windows and specifically with Visual Studio. I am currently trying to get used to compiling my code under linux.
Although I still have a lot of documentation to read, and don't expect you guys to make it too easy for me, it'd still be nice to get some pointers on where to start. I have some specific questions, but feel free to suggest/recommend anything else regarding the subject.
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
Any help, links to guides & documentation (preferably those that are aimed at beginners) are very much appreciated!
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
You build from the makefile by invoking "make". And inside your makefile, you compile and link using g++ and ld.
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
It's a script usually used to set up various things based on the environment being used for building. Sometimes it's just a basic shell script, other times it invokes tools like Autoconf to discover what is available when building. The "configure" script is usually also a place for the user to specify various optional things to be built or excluded, like support for experimental features.
How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
ld is the GNU linker. You can invoke it separately (which is what most makefiles will end up doing), or you can have g++ delegate to it. The options you pass to g++ and ld determine where to look for included headers, libraries to link, and how to output the result.
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
Vim and Emacs are very flexible editors that support a whole bunch of different usages. Use whatever feels best to you, though I'd suggest you might want a few minimal things like syntax highlighting.
Just a note to go with MandyK's answers.
Creating make files by hand is usually a very unportable way of building across linux distro's/unix variants. There are many build systems for auto generating make files, building without make files. GNU Autotools, Cmake, Scons, jam, etc.
Also to go more in depth about configure.
Checks available compilers, libraries, system architecture.
Makes sure your system matches the appropriate compatible package list.
Lets you specify command line arguments to specialize your build, install path, option packages etc.
Configure then generates an appropriate Makefile specific to your system.
What are recommended guides on
creating a make file, how do I compile
from this makefile (do I call g++
myself, do I use 'make'?)
I learned how to write makefiles by reading the GNU Make manual.
Looking at other linux software, they
almost always seem to have a
'configure' file. What exactly does it
do? Does it only check if the required
libraries are installed or does it
more than just checking requirements?
The configure file is usually associated with autotools. As the name of the script suggests, it allows you to configure the software. From the perspective of the developer this mostly means setting macros, which determine variables, which libraries are available, and such. It also tests for the availability of libraries. In the end the script generates a GNU Makefile, which you can then use to actually build and install the software.
The GNU build system is only one of many. I don't particularly like the GNU build system as it tends to be slower than others, and generates an ugly Makefile. Some of the more popular ones are CMake, Jam (Boost Jam might be of interest for C++) and waf. Some build systems simply generate Makefiles, while others provide a completely new build system. For simple projects writing a Makefile by hand would be easy, but "dependency checking" (for libraries, etc) would also have to be done manually.
Edit: Brian Gianforcaro also pointed this out.
Your question is a bit too general, but here is what I would recomment:
Editor: vim and emacs are popular. What matters the most, as with most tools, is to master one. I like using vim because vi (its descendant) is available everywhere, but that may not be very relevant, specially if you stay on Linux. Any programming editor is fine.
configure: unless you do big projects, don't bother with it. It is a nightmare to use and debug. It only makes sense if you intend to distribute your project - in that case, read the autobook: http://sources.redhat.com/autobook/. As other said, there are alternatives (cmake, scons, etc...). I am quite familiar with both scons and autotools, but I still use make for small (couple of files) projects.
Concerning shared library: it is almost as windows, except that you link against the shared library directly - there is no .lib vs .dll distinction in Linux. For example. for one library foo with a function foo:
int foo(void)
{
return 1;
}
You would build it as follows:
gcc -fPIC -c foo.c -o foo.o
gcc -shared foo.o -o libfoo.so
A main (of course in real life you put the API in a header file):
int foo(void);
int main(void)
{
foo();
return 0;
}
And then, you link it as:
gcc -c main.c -o main.o
gcc main.o -o main -L. -lfoo
The -L. is here to say that you want the linker to look in the current directory (contrary to windows, this is never done by default in Linux), the -lfoo says to link against the library foo.
So to get you started I will first point you to this guide for makefiles, it also covers some linking stuff too.
It's just a little something my university Computer Science prof gave us I found it to be very clear and concise, very helpful.
And as for an IDE, I use eclipse usually because it handles the makefile as well. Not to mention compile and standard output are right at your fingertips in the program.
It was mainly intended for Java developing, but there is a C/C++ plugin too!
Recommendations for code editors? I am
currently using nano and I've heard of
vim and emacs, but don't know what the
benefits of them are over eachother.
Are there any others, and why would I
consider them over any of the previous
three? Note: I am not looking for an
IDE.
Vi and Emacs are the two quintessential Unix editors; if you are set on using a text editor rather than an IDE, one of them or their derivatives (vim, xemacs, etc) is the way to go. Both support syntax highlighting and all sorts of features, either by default or via extensions. The best part about these editors is the extensibility they offer; emacs via a variety of lisp, and vim via its own scripting language.
I personally use Emacs, so I can't say much about Vim, but you should be able to find lots of information about both online. Emacs has several good tutorials and references, including this one.
EDIT [Dec 2014]: There seems to be a trend of cross-platform and highly extendable editors recently. This could be a good choice if you'd like something less than an IDE, but more graphical than vi/emacs and native-feeling across multiple platforms. I recommend looking at Sublime or Atom; both of these work across Windows/Linux/Mac and have great communities of plugins and themes.
I recommend the book The Art of Unix Programming by ESR. It covers choice of editor, programming language, etc. It also gives a good sense for the mindset behind programming on Unix or Linux.
For editors, you probably want either Vim or Emacs. They are both different and which one is better is more about personal taste than anything else. I use Vim. It is great for quickly moving around the code and making changes. I didn't like Emacs as much but many people do. Emacs is extremely extensible and can be used for everything from a news reader to an ide. Try both and see what you like.
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
If you're using Linux with a window manager (KDE, Gnome, etc.) you could also consider using the standard text editor for your window manager. The main benefit it would have over vim/emacs/nano is that it would seem more familiar to one coming from a Windows environment - an editor written to run on the window manager has a menu bar, file open/save dialogs, undo/redo, and plenty of other neat features that console editors probably can't match. (Though emacs and vim are pretty sophisticated these days, so who knows ;-P)
On KDE (which is what I use) I can recommend KWrite, which is a well-featured but fairly basic text editor with syntax highlighting; or Kate, which is a fancier text editor with some extra features: session management, a builtin terminal panel, automatic invocation of make, and several plugins including a C/C++ symbol viewer. I usually use Kate for my C++ work when I don't want to bother with setting up a full IDE project. (FYI the IDE for KDE is KDevelop)
the space between invoking g++ directly and using an autotools build chain is pretty narrow. Get good at autotools, which is really the closest thing to a 'project' available in the Linux/Open Source world.
For someone coming from Visual Studio, all this commandline stuff might seem arcane and messy.
Before you turn into a bash shell/vim/emacs junkie, try a few GUI based tools first so you have some transition time...
QT 4.5 with its QT Creator mini-IDE. This is the best framework, lightyears ahead of the competition.
Eclipse (C++) - From my experience with this on Windows, I find it's astounding ( This is probably the best Java application ever written )
KDevelop
Anjuta
If you use Delphi, Lazarus/FreePascal is a good alternative.
I'm sure the longhairs will scoff and claim that vim or emacs gives them the best and fastest development environment, but different strokes for different folks. Someone accustomed to an IDE will take some time to switch or may not wish to switch at all.
For all their editing prowess, creating GUI apps is certainly not a job for 80x25 tools.
It takes years to become an expert with the command line side of things, its more of a transformation of worldview than anything else.
As a side note amongst the proper answers here.. In case you wanna hit the ground running as a Windows guy, I'd suggest the fresh new Qt SDK. It will feel like home :-)
I advise using SCons in place of Make, it does the same job but it's easier to use and handle out of the box how to make dynamic libraries, dependencies, etc. Here it is a real life example for a simple prog
env = Environment()
env.Append(CCFLAGS='-Wall')
env.Append(CPPPATH = ['./include/'])
env.MergeFlags('-ljpeg')
env.ParseConfig("sdl-config --cflags --libs")
env.ParseConfig("curl-config --cflags --libs")
env.ParseConfig("pkg-config cairo --cflags --libs")
env.Program('rovio-pilot', Glob('./src/*.cpp'))
As a text editor, I'm happy with JEdit for coding, but it's a matter of taste.