I want to set an alias in powershell to compile my c++ file using the command
g++ -std=c++17 -O2 any_file_name.cpp -o any_file_name -Wall
I want to map this command to "co" so that when I type any co any_file_name it should compile. Is there a way of doing this ?
Related
The g++ command doesn't work after declaring a variable before the g++ command.
For example, the following works:
echo "Train LINE starts"
g++ -lm -pthread -Ofast -march=native -Wall -funroll-loops -ffast-math -Wno-unused-result line.cpp -o line -lgsl -lm -lgslcblas
But this does not:
PATH="foo path"
echo "Train LINE starts"
g++ -lm -pthread -Ofast -march=native -Wall -funroll-loops -ffast-math -Wno-unused-result line.cpp -o line -lgsl -lm -lgslcblas
Instead, I get an error: g++: command not found. Is it the correct way of declaring variables?
The PATH variable holds the path to your programs witch the shell is looking for. So if you change that, the shell doesn't find the program. You can add an folder by using
PATH=/usr/local/progdir:$PATH
export PATH
The PATH environment variable consists of a colon-delimited list of directories. The shell searches for executable files in these directories in response to commands issued by a user.
For example, if g++ executable is located in /usr/bin directory, then you can implicitly call /usr/bin/g++ only if PATH contains /usr/bin:
echo $PATH
/usr/bin:/bin:/opt/bin
If you want to add a search path, you should append it to the existing value of the variable:
PATH="$PATH:/new/search/path"
In your example you redefine PATH which is used to lookup the directory where g++ lives. Use a different variable name.
If you want to append a new path in PATH variable, use this:
PATH=$PATH:foopath
instead of:
PATH="foo path"
If you are using PATH as variable for anything else, use another name.
i'm newbie use MinGW64 and msys2.
i have write simple program, just output "hello,world", but it links a dll for test.
#include <stdio.h>
int main() {
printf("hello, world\n");
return 0;
}
i run command in terminal like this:
$ gcc -g -Wall -I/usr/local/include -L/usr/local/bin -llua53 --shared -o test.dll main.c
it's works well.
but i write a Makefile use same command like this:
all: main.c
gcc -g -Wall -I/usr/local/include -L/usr/local/bin -llua53 --shared -o test.dll main.c
the error was output:
$ mingw32-make.exe
gcc -g -Wall -I/usr/local/include -L/usr/local/bin -llua53 --shared -o test.dll main.c
E:/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -llua53
this problem confused me, what the difference of command process from terminal input and from Makefile?
mingw32-make.exe is for use with the windows command shell and doesn't understand POSIX paths, you need to use make.exe.
I need help it is bug or I don't understand how compilation options are working ?
I have sample main.c file and try to compile it as:
$ g++ -nostdlib -g -fno-rtti -fno-exceptions -ffreestanding -nostdinc -nostdinc++ -Wl,--build-id=none,-g,-nostdlib,-nostartfiles,-zmax-page-size=0x1000 main.c -o main
and as output I have this:
$ ls
main.c startfiles
I am trying to understand why g++ created file named "startfiles" not "main" ?
If you read the GNU ld official documentation you will see that there is no option named -nostartfiles. What you do pass for arguments to the linker with that is the options -n and -ostartfiles.
If I were you, I would check those other options you try to pass to the linker as well.
-nostartfiles is a compiler flag as far as I know, not a linker flag.
For the linker, it's the same as -n -o startfiles, which is why you're getting that output file name.
I am trying to run program from the Learn C Hard Way book
I need to pass the library filename 'build/liblcthw.a' as the last parameter.
For eg :
Doesnt Work on Ubuntu :
gcc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG build/liblcthw.a tests/list_tests.c -o tests/list_tests
Works on Ubuntu :
gcc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/list_tests.c -o tests/list_tests build/liblcthw.a
How do I handle this in Makefile ? CFLAGS will only add it before the source filename and it doesnt work. How do I force CFALGS to add the library filename at the end of the command ?
CFLAGS are flags for the C compiler. Libraries typically go into a variable called LDLIBS. Set LDLIBS=build/liblcthw.a and see if that works.
The first invocation doesn't succeed because the order of sources and libraries in the command line is wrong. The correct order is source files, then object files, followed by static libraries followed by dynamic libraries.
I have a batch file to compile and link all of my code. It contains the following:
#echo off
nasm -f aout -o start.o start.asm
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o scrn.o scrn.c
ld -T link.ld -o kernel.bin start.o main.o scrn.o
pause
Problem is, when i run it it just prints all this out as text.
It's definitely a batch file. it has the .bat file ending, and in notepad++, the syntax for #echo off and pause are being highlighted without being set manually.
Is this a windows 7 bug? or am i doing something wrong?
Are you running from a command line or by double-clicking in Explorer?
Maybe you have the Edit action set as the default?
Try right-clicking and selecting Open.
EDIT: Maybe your line terminators are messed up. Windows expects CRLF.
In Notepad++:
Click View->Show End Of Line.
If they are not CRLF, click Format->Convert to Windows Format.
A simpler example
Windows 7 installed
Using cygwin to get gcc
I used the following batch script:
PATH = %PATH%;C:\cygwin\bin;
gcc test.c
on the following c file:
main() {
printf("hello, world");
}
And it compiled fine.
My Conclusion
Windows 7 batch scripts work pretty much like in previous versions of windows.
Simple things that could trip you up
Gcc is not installed by default on Windows, I suggest either cygwin or mingw
Gcc is not on the PATH by default after you have installed gcc, you can add it to the system's environment variable or add it in your batch script (using something like the first line of the batch script I just used).