Makefile for compiling c++ program - c++

I'm using g++ on linux and writing multiple cpp programs. For ex. I have ex1.cpp, ex2.cpp, bot.cpp.
All these cpp programs are inside the same folder. I want to use make to compile individual programs as in "make ex1" should compile ex1.cpp and not the other cpp files. "make bot" should only compile bot.cpp
The compilation command I intend to execute is :
g++ -o bot bot.cpp
I don't want to write target and dependency in MakeFile for every cpp program I create in the folder and I don't want to compile all cpp's at one go using *.cpp.
Is it possible first of all to achieve this ? Is yes please suggest a solution

A simple solution to your question :
Just run make ex1 in the command line and it will compile using the command g++ ex1.cpp -o ex1.
If you want to dynamically include libraries/shared objects or add additional flags then you should create a make file or follow the procedure suggested by Lukasz (Too complicated though).

You don't need to write a makefile. make ex1 should already do what you want.

Related

Compile multiple C++ files with SublimeText3

I was using a .bat to compile my C++ files but I would like to find a Build System for ST3 who is able to find alone (throught #include <header.h> ) which files needs to be compiled
I found some build system who can build your C++ files, but only if you put the right name. In that case that's almost easier to edit the .bat file when needed..
Is
#echo off
g++ main.cpp function.cpp -o start.exe
start.exe
pause
a good g++ syntax?
Do someone have the solution?

How to add environment variable into Makefile?

Since I am using IDE instead of GNU writing codes, I am having a hard time reading those documents about Makefile, so far I couldn't find any documents states clearly where to put $BOOST_HOME. (I am using Boost Library for my project so it needs to compile with it)
so far I am compiling the program with this command:
g++ -Wall -std=c++11 -o ladder Source1.cpp -I/home/s/suw/boost_1_65_1
since this will be compile in other's computer so the path of boost library will be different thus I need to put $BOOST_HOME into Makefile, any help would be appreciated.
EDIT:
So I think I need to set $BOOST_HOME a path and put it into makefile, but path is varies by different user, how to let somebody run my program by just using a simple:
g++ *.cpp -o foo
???

Using gcov to test a c++ program

I am using gcov for the first time to analyze my program (C++)
The program consists of three classes and I have built the project using Code::Blocks.
When I am invoking the program using the following command:
C:\Users\XXX\Documents\Test\TreeObjModel\src>gcc
-fprofile-arcs -ftest-coverage Tree.cpp
I receive the following error:
Tree.cpp:1:18: fatal error: Tree.h: No such file or directory
compilation terminated
While the cpp files are in the directory "C:\Users\XXX\Documents\Test\TreeObjModel\src\" , the header files are in directory "C:\Users\XXX\Documents\Test\TreeObjModel\include\"
Do we need to have both the code and header files in the same directory?
Thanks in advance.
You should use the -I flag to specify where your header files are.
Judging from your example, you should add -I../include
You have at least two options to instruct the compiler where to find the header files (includes).
-Ipath_to_includes as parameter for gcc compiler. E.g. -I../include
When including in your program, specify the directory. E.g. #include "../include/foo.h"
My strategy would be to just compile my project successfully and only then try to use some other stuff, like flags for code coverage. I say this because your error does not have anything to do with gcov, and trying to instrument your program to get code coverage before your program even compiles, makes things more complicated for you. One step at a time ;)

Make G++ use my lib automatically

I have an already built library made of this files:
A bunch of headers.
A .so file (libmylib.so).
I want to compile a c++ program (sample.cpp), where I included the headers, and where I need to use the library. This is what I've done, and it's working:
Put the headers in usr/local/include.
Put the .so file in usr/local/lib.
Compile the program in this way: g++ sample.cpp -lmylib.
My question is: why is it not working if I omit -lmylib from the last line?
Is there a way to install the library such that I don't need to put it every time in the g++ command?
Thank you.
What libs are used by default depends on some setting in the compiler/linker,
but it´s not "every lib in usr/local/lib" or any directory, just some specific names
(or even just a single one). Call g++ -v or g++ -dumpspecs to list it (and more stuff)
So, either rebuild your compiler with your own lib list, or specify it manually everytime.

Using alglib without compiling every time

I'm using some methods from the c++ AlgLib library. In other libraries I've used there were some instructions for installation, after which I could include .h files and compile with -l (e.g. using the GMP-library and compiling with -lgmp). However with alglib the only thing I've been able to get running is using a makefile and compiling all needed .cpp files - every time I compile my program. Here is an example of a makefile:
all:
g++ name.cpp ap.cpp integration.cpp interpolation.cpp alglibinternal.cpp linalg.cpp
alglibmisc.cpp specialfunctions.cpp solvers.cpp optimization.cpp -o name
As compiling all these files every time is relatively time consuming, is there a way to avoid it?
You can use the make feature.
Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.
View the page: http://www.gnu.org/software/make/ to use this tool.