Including header from the same file on every platform - c++

I have a file cpp-options.txt in which I have written every compiler option I use to compile my C++ programs.
I have made an alias g+ as g++ #/path/to/cpp-options.txt $* , so that whenever I invoked g+ prog.cpp, from anywhere on my computer, the program is compiled with all the compiler options from that file.
Now I want to add another option which includes a header file header.h in the options file. This file is always kept on the same directory as the cpp-options.txt file.
So, now the cpp-options.txt file looks like this -:
-Wall -Wextra.....
-include /path/to/header.h
Now, this setup works on Windows perfectly, but wont work on Linux, as the absolute path to the options file on Linux would be something like this -:
/mnt/media......../absolute/path/to/header.h
So, the compiler would complain about the absence of any such file on Linux.
Now I am aware of one solution of this problem, that is to include the folder in which these two files are kept in the PATH environment variable on both the Operating Systems and then simply writing -:
-Wall -Wextra.....
-include header.h
However, I dont want to pollute the PATH variables.
Is there any other way of accomplishing this ?

The best I could do was to create another common file cpp-options-common.txt which contained only the the compiler options (-Wall, -Wextra, -std=c++14 etc), and shift the -include /path/to/header.h statement to the cpp-options.txt file.
Also, I imported the cpp-options-common.txt file into the cpp-options.txt file by using the # GCC compiler directive.
My final configuration -:
cpp-options-common.txt -: ( located in the Windows partition )
-Wall -Wextra -Wfatal-errors ...
On Windows -:
cpp-options.txt -:
#path/to/cpp-options-common.txt
-include path/to/header.h
Linux -:
cpp-options.txt -:
#/media/Data/path/to/cpp-options-common.txt
-include /media/Data/path/to/header.h

Related

G++ build exe file in specific directory

I was lately using SDL and now i want to put the .exe file in a specific directory so it looks cleaner, somebody knows how to do that?
When compiling with g++, gcc, or many other similar compilers, you specify the output file with the -o flag.
For example, to set the output file as foo.exe in the parent directory, you would call g++ like this:
g++ [other options/source files here] -o ../foo.exe

What does the "#" symbol mean in a makefile when after an -I flag such as -I #mathinc#?

I'm trying to understand the following line in a Makefile.in file:
CXXFLAGS += -O3 -DNDEBUG -std=c++11 -Wno-deprecated-declarations -Isrc -I #mathinc#
I know the -I flag adds a directory to the list of places where the compiler will search for included files but what does #mathinc# mean?
Note that the file is called Makefile.in -- this signifies that it is input to another file (or transformation).
In short, configure will run and determine, say, where the relevant include files are for #mathinc -- likely some math headers. After you run configure it will produce Makefile (no trailing .in) based on what it finds. Do inspect that file.
configure scripts are created in a system called autoconf which, like all build systems, has its fans and its haters. There are some decent tutorials as for example this one.

Atmega, avr-gcc, assembly include file from another directory

I can't convince avr-gcc on windows to include a *.h file from another directory:
>avr-gcc -Wa,-gdwarf2 -x assembler-with-cpp -c -mmcu=atmega256rfr2 halW1.S
C:\Users\me\AppData\Local\Temp\ccjzoYpN.s: Assembler messages:
C:\Users\me\AppData\Local\Temp\ccjzoYpN.s:6: Error: can't open halGccD.h for reading: No such file or directory
The required file is one up level in ../include folder
(this is the BitCloud stack provided by Atmel itself)
I tried as Atmel Studio does to pass include folder:
>avr-gcc -Wa,-gdwarf2 -x assembler-with-cpp -c -mmcu=atmega256rfr2 halW1.S -I "..\include"
But seems that avr-gcc assembler ignores the -I option. I tried with relative, absolute, even put that path in global PATH.
If I copy required *.h in the same folder where *.S file resides, it's working.
What is wrong?
Ok, found'it by mistake.
In case anyone needs, -I is not working alone for assembly files. When using avr-gcc as assembler, explicit assembler (-Wa) or linker (-Wl) directives must precede others. Such as:
-Wa,-I"..\..\path_to_h"
Also pay attention to backslash (not slash)... the old windows problem.
Seems that avr-gcc should parse correctly, but is not.

Makefile configuration for entire system

As we know, in the appliance when we use the command
make [file-name]
It automatically compiles with some flags:
-ggdb -O0 -std=c99 -Wall - Werror
I need to know in which directory the CS50 edited Makefile is located, because I want to configure my own Makefile for the entire system by which I can make any .cpp file.
When I compile c++ file with make it automatically compiles with g++ but I want to compile .cpp file with clang++ compiler, adding some essential flag such for -g for debugging -O0 for assembly code.
I'm asking how to create a Makefile for that specific reasons, if possible.
Make uses Makefiles in the current directory and Implicit-Rules. You can modify the behavior of implicit rules by changing the variables that those explicit rules use.
For example, to change the compiler for .cpp files, you could set your own CXX variable, either
in the environment (Make uses it):
CXX=clang++ make [file-name]
#OR
export CXX=clang++; make [file-name]
in a local Makefile:
CXX:=clang++
#The implicit rule, which you'll find in the link, takes care of the rest

qmake: compile single cpp file into an executable

I got a project in QT and I'd like to use qmake for compiling one additional cpp file that (into standalone executable) is not connected in any way to my application or even QT (it's very simple plain C++ program). Is there any way to do this without rebulding whole project structure? Do I need separate .pro file for every executable or is there any other way for simple compiling just one, plain C++ file?
As you may know qmake -project will make one .pro file with the name of the folder containing your whole source and header files, if you qmake this pro file then make your project you will get compiled .o file from your new cpp file even if it's not connect to your Qt project directly.
but if this file got main() function of course you will have multiple main() definitions error by compiler.
you will need to rebuild that file of course
as you know for simple compiling of only one standard plain c++ file you just
g++ source.cpp -o excutable_name.exe
for more strict compiling with two steps:
g++ -Wall -pedantic -ansi source.cpp -c compiled_file_name.o
g++ compiled_file_name -o excutable_name.exe
but if you are going to use for example a code related to Qt, you have to include Qt headers and link necessary libraries :
g++ -Wall source.cpp -c compiled_file_name.o -L qt/library/path -lQtGui -lQtCore -lQtother_necessary_libraries -I Qt/include/path -I Qtother_necessary_include_paths
To make an additional executable you can use use system() in .pro like so:
system(g++ otherapp.cpp)
Which will be built every time you call qmake. However if you want to build the additional app automatically when its source is changed, use QMAKE_EXTRA_TARGETS instead.