Compile multiple C++ files with SublimeText3 - c++

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?

Related

C++ package cannot find header locations called from other header files in package

Before I start off, please bear with me, I'm very new to cpp with no formal training, and this question may have been asked and answered already, but I'm not sure what to call anything.
So I have a cpp program where I want to use this open source google sling package in. I have the google sling package in the same directory as my main.cpp program, and I can include one header with the path relative to the program, but that header(A) calls other headers(B:) within the package and the relative path to those headers(B:) is not relative to that header(A) file. Here is the error stack:
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ g++ -Isling main.cpp
In file included from main.cpp:7:0:
sling/frame/object.h:25:10: fatal error: sling/base/logging.h: No such file or directory
#include "sling/base/logging.h"
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ ls
bin obj test_project.cbp test_project.layout
main.cpp sling test_project.depend text_testfiles
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ vim main.cpp
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ cd sling/base
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project/sling/base$ ls
bitcast.h flags.cc libinit.cc port.h status.cc types.h
BUILD flags.h logging.cc registry.cc status.h
clock.cc init.cc logging.h registry.h strtoint.cc
clock.h init.h macros.h slice.h strtoint.h
So sling/base/logging.h is actually there, but since it's being called from sling/frame/object.h, the correct relative path would be ../base/logging.h(at least my limited knowledge tells me so). I think I have to set it up so that it's part of the global path that my cpp compiler searches in for dependencies. Either way I've done something terribly wrong.
I'm looking for a semi quick fix, but also I'd like to avoid this in the future, so a link to the appropriate information would be very much appreciated as well.
Edit:
Also tried with same error:
g++ -I.sling main.cpp
g++ -Itest_project main.cpp
g++ -isystem sling main.cpp
g++ -iwithprefixbefore "/home/.../test_project/" main.cpp where ... is the path from home to my test project
Thank you melpomene, the correct answer was g++ -I. main.cpp. Case closed.
Thanks to Jerry Jeremiah in the comments for this info:
The #include <x> says "include the file from the include path" and the #include "x" says "include the file from the current directory"
Thus, my program was calling the header file "sling/base/logger.h" from the directory "sling/frame/" (which is where the originally called "sling/frame/object.h" lives) and for obvious reasons couldn't find it there.

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
???

cygwin: no header file found

The following problem: Cygwin or Visual Studio give the error.
CGAL/Splitters.h: No such file or directory
This error appears for every header! The header is implemented by the code
#include <CGAL/Splitters.h>
The error disappears, if I change the code, such that I write the whole path:
#include <c:/path1/path2/CGAL/Splitters.h>
But this is no solution which satisfies me, because I would have to change hundreds of such code fractions.
I think it should be a problem of Visual studio or cygwin. In cygwin I wrote the command:
$ g++ -std=c++11 example.cpp -o example
What is the reason for the error? How can I fix it?
Please give easy understandable instructions, since I am a beginner in C++.
The compiler does not know where to look for the header file
referred to by #include <CGAL/Splitters.h> unless you tell it,
because that header file is not located in any of the compiler's
default search directories for header files.
You tell the compiler where to look by passing it an -I option:
$ g++ -I/path/to/cgal/headers -std=c++11 example.cpp -o example
where that header file will be:
/path/to/cgal/headers/CGAL/Splitters.h
Further reading: An Introduction to GCC - for the GNU compilers gcc and g++
Later
So for every of the header files I have to write the - I option?
No. -I/path/to/cgal/headers/ by itself will of course tell the compiler
where to find every CGAL header file used in your program.
You can check include path in Visual Studio :
Tools > Options > Projects and Solutions > VC++ Directories > Include files
So, check default header file path.
I hope this can help you.

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 ;)

Makefile for compiling c++ program

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.