I need to select some command-line parameters variable with name of file such as:
g++ [options] ${something}.s [other parameters] foo.cpp
to have assembly output same as foo.s for foo.cpp and bar.s for bar.cpp for all files of project.
Where can I find the necessary environment variable ${something}?
Related
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
I'm a guy who came from python to cpp. I barely have any experience with gcc compiler.
The problem I have is;
if I have foo.h under somedir. Compiling bar.cpp by g++/gcc can't find foo.h unless I include the absolute path.
some metadata
os : ubuntu 20.04 LTS
gcc version : gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
(note: I have tried gcc -B . bar.cpp but doesn't work)
If the *.h file is in the same directory as the *.cpp file then gcc will find the file automatically in that same directory so you can use this command.
gcc bar.cpp
If the *.h file is in a different directory as the *.cpp file then gcc needs to be given more information where is the header file located, so you need to give the absolute path to the -I option.
gcc -I somedir/foo.h bar.cpp
Please note that in both these cases you can just write #include "foo.h" and the code will just work. In the first case gcc will be able to find the file because it is in the current working directory. And in the second case gcc will be able to find the file because it was given the absolute path to the directory where that file is located. You only need to specify the name of the file in the #include not the path since gcc already knows the path.
I am trying to generate output file by the use of macro in source file.
Whatever a macro name is, generate the final .exe file by using the macro name.
#include <iostream>
#define Apple
//#define Banana
//#define Mango
int main()
{
...
}
How could i generate an output file name like Apple.exe ?
compiler: g++
OS: windows
You cannot steer the name of the final linker artifacts (executable in your case) from within the source code.
This needs to be done using the -o <filename> linker flag, thus in your case
> g++ -o Banana.exe main.cpp -DNAME=Banana
To control this more easily you can define these as variables in a makefile, e.g. like
# Comment the current, and uncomment a different definiton to change the executables
# name and the macro definition for NAME
FINAL_NAME = Banana
# FINAL_NAME = Apple
# FINAL_NAME = Mango
$(FINAL_NAME).exe : main.cpp
g++ -o $(FINAL_NAME).exe main.cpp -DNAME=$(FINAL_NAME)
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
I'm doing some experiment about extern keywork and currently working on Fedora 19 and g++.
Im trying to compile multiple c++ files and execute the program. Here it compiled successfully. but when try to run it shows Permission denied error. I change to permission using chmod to 777. but still result is same. then I try to compile and run a simple Hello World program and That works fine. Whats wrong with my program?
test1.h
int a=0;
test2.h
#include <iostream>
using namespace std;
extern int a;
void foo()
{
cout << "This is a test string " << endl;
cout << a << endl;
}
int main()
{
foo();
}
Here is how I compiled and the execute result of theTerminal
[root#localhost cpp]# g++ test1.h test2.h -o test.o
[root#localhost cpp]# ./test.o
bash: ./test.o: Permission denied
[root#localhost cpp]#
You have several problems. If you use the file(1) utility to inspect the output file, you'll realize why it can't be executed:
$ file test.o
test.o: GCC precompiled header (version 013) for C++
Precompiled header files are not valid executable files—they're not valid ELF files and they're not script files with a shbang line, they're just data files that the compiler knows how to read.
Why are you getting a precompiled header file? Because you're asking g++ to compile header files (.h files). It's really ridiculous to give your C++ source files .h extensions, because they're not header files, they're source files. Give them the proper .cc or .cpp extensions, and g++ will compile them correctly to an executable.
Secondly, why are you giving the output file the name test.o? .o is used for object files (compiled versions of singular source files, not complete executables), but you're asking g++ to compile a full executable. If you only want to compile and not also link, then pass the -c flag, and then manually link the object files together. Don't name your executables with .o. test would be the more proper name for an executable compiled from a source file named test.cc, but I'd caution against that and recommend using something else, because test is also the name of a shell builtin function.
When you compile .h files using g++, the compiler generates precompiled header files. These are not executable files. They won't run even if you changed their permissions to 777.
Look for This program is also useful when precompiling a C header file with a ‘.h’ extension for use in C++ compilations. at https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b.
Try these commands:
g++ -o test.o test1.h
file test.o
You should get the output
test.o: GCC precompiled header (version 014) for C++
To create an executable, rename the files to test1.cc and test2.cc. Then, build the executable from them using
g++ -o test test1.cc test2.cc
Now, you will be able to execute the program using:
./test