Passing arguments from my bash script into a c program - c

Im trying to use one of the inputs from my bash script into my c program and i just dont know how to do so.
What command should i add for the argument to be passed on to my c program?
Thanx
My script is -
bash script

I guess that you want to pass the argument to prog.exe and not to gcc, then
gcc -c -g -Wall calc_statistics.c gcc -o prog.exe && ./prog.exe "$1"
That is, compile the program and if there are no error run it with the argument.
It's not very efficient to compile the program every time the script runs so you can move gcc out.
You can also check your script using shellcheck.

Related

How to run C program without having to run 'gcc filename.c' and 'a.exe '

When i type the command gcc filename.c a new file 'a.exe' is created, then i have to run a.exe to get my program to run.
Is there a way just to type one command to run my program or can you run a C program without having a new .exe file being created?
I use gcc version gcc (MinGW.org GCC-6.3.0-1) 6.3.0
(Complete beginner with C)
You can't run the .c file directly - you have to compile it into an executable (using gcc or whatever compiler). However, you don't have to compile it every time you want to run it - you only have to compile it after first creating it and after you make any changes. So you can run gcc once, then run a.exe multiple times.
You can name the executable something other than a.exe if you wish using the -o option:
gcc -o prog filename.c
./prog
You could make a .bat file that does everything for you. For example:
build.bat
gcc %1.c -o %1.exe
%1.exe
This will build and run the file for you:
build hello
will compile and run hello.exe for you.

How to disable all compilation arguments for gdb compile code command?

Following this process
from an earlier question (see answer).
gdb is a huge improvement over spim, but I'd like to use the compile code feature of gdb, to inject arbitrary mips instructions at the point of execution.
I have read Compiling and injecting code in gdb. When I run run compile code <anything>, I get the error "compilation failed, unrecognized argument -m32". Then when I run set debug compile in gdb, and I try compile code <anything> again, I see that the argument -m32 is passed to mips-linux-gnu-gcc.
I tried overriding the compilation arguments using set compile-args -march=mips32r3, which adds the compilation argument, but -m32 is still passed and still gives me an error.
How do I prevent -m32 from being passed? Is there a clean workaround (short of making a dummy script that strips -m32 before compiling?)
How do I prevent -m32 from being passed?
It looks like this is the case where GDB developers thought that "GDB knows better", and didn't provide any way to override this.
Specifically, the -m32 comes from default_gcc_target_options(), which unconditionally adds -m%d with gdb_arch_ptr_bit() as the argument.
Short of building your own GDB or providing a GCC wrapper that strips -m32, I don't see any way to get rid of this argument.
Make this script, special-gcc. Make it executable, chmod 777 special-gcc. The script uses exec to have the process replaced with the gcc invocation, as opposed to spawning a child process. Arguments are $#, stored in array, filtered in loop, then passed to the gcc invocation.
#!/bin/bash
declare -a args=()
#!/bin/bash
echo -- "----------------" >> ~/gcc-wrapper-log
for arg in "$#"
do
if ! [[ "$arg" == '-m32' ]]; then
echo -- "$arg" >> ~/gcc-wrapper-log
args+=("$arg")
fi
done
exec mips-linux-gnu-gcc -static "${args[#]}"
Inside gdb, run the command set compile-gcc /path/to/special-gcc. Attempt some command compile code <anything>. Then in gcc-wrapper-log you can see all the arguments to the compilation, can selectively disable them in the script.
For me, the compilation succeeded, but because I am using the mips-linux-gnu-gcc cross compiler binary, gdb seems not to link the resulting .o file correctly. See the docs for details on the internals of the compile code feature. Somewhere in the steps "Relocating the object file" is where the process failed for me for mips-linux-gnu-gcc.
However, this is still a clean and easy way to precisely control the compilation arguments used by gdb compile code.

Cmd prompt windows compiling

For my WINAPI projects, I am using atom IDE with c programming, I could compile my code from cmd prompt without a problem until I have started using .rc files. but now that I am using rc files before compiling my program I need to run these commands on the cmd prompt.
gcc -c jake.c
gcc -o jake jake.o -mwindows
windres -o jakerc.o jakerc.rc
gcc -o jake jake.o jakerc.o -mwindows
typing them all again and again to see if my program works correctly each time is really tedious. (Also please do not tell me to use IDEs like DEV c++ or Visual Studio because I don't like them as much as I like atom.)
so I have came up with this solution. I have made an extra file called compile.c which inside it looks like this.
#include <stdio.h>
#include <stdlib.h>
int main() {
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -c jake.c");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -o jake jake.o -mwindows");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul windres -o jakerc.o jakerc.rc");
system("C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -o jake jake.o jakerc.o -mwindows");
return 0;
}
When I compile and run this program I get this error:
'C:\\Users\\hashtag\\Desktop\\rawsock\\kokul' is not recognized as an internal or external command,
operable program or batch file.
How do I get rid of this error, and how do I automatically compile my files when I run compile.exe?
as mentioned in my comment, i'd recommend using a batchfile and not a compiled c program for this.
Example:
#ECHO off
SET workingDirectory=%~dp0
SET fileName=%1
echo using %workingDirectory% as working directory
gcc -c %fileName%.c
gcc -o %fileName% %fileName%.o -mwindows
windres -o %fileName%rc.o %fileName%rc.rc
gcc -o %fileName% %fileName%.o %fileName%rc.o -mwindows
you could run your desiered commands with nameofthebatchfile.bat jake
As long as kokul is a directory the command line
C:\\Users\\hashtag\\Desktop\\rawsock\\kokul gcc -c jake.c
Doesn't make sense.
You can combine multiple commands. If you want to change into the directory kokul and start the compile this will work
CD C:\\Users\\hashtag\\Desktop\\rawsock\\kokul & gcc -c jake.c
But anyhow writing a batch file is much easier:
You can do it with two lines
windres -o jakerc.o jakerc.rc
gcc -o jake jake.c jakerc.o -mwindows
Save them to file "mj.bat", then run form command prompt by typing "mj".
When your project grows beyond two or thre files, you should use makefile
GCC and Make Compiling, Linking and Building C/C++ Applications
Check also atom-shell-commands or run-command package for Atom. You can run this directly form editor.
If you're working with command-line tools, this task just seems to cry out for a Makefile. Not only will make automate the necessary actions, it will try to work out from the file timestamps which actions actually need to be carried out. If you haven't changed a C source file, for example, you don't need to recompile it. The effectiveness of make, or similar, will increase enormously as your applications get larger and have more complicated dependencies between components.
You can do some of this stuff with simple batch files but, ideally, the batch file needs to stop when any of the steps encounters an error. There's no point carrying on a long build process that is doomed to fail, and finding the relevant error message in pages of irrelevant output can be a chore. This is another thing that make just does right.
It can take a while to get on top of Makefiles, with their arcane syntax. However, the effort will be rewarded many times over in the longer term.

Can a C program be executed, in the same makefile as it is being built?

I have a makefile which builds my .C project, and produces an executable file. I know this can then be run from using the command ./(NAME) from the terminal.
My question is this - is it possible to actually build AND run within the makefile, so that i would just be able to type 'make' in the terminal, and the program would be built and run, all from this one file & command? (without the need of manually running the executable file).
Thanks :)
No idea what is you makefile, but you could do something like that:
PROGARGS=
.PHONY:all run
all:prog
#...
run:all
./prog $(PROGARGS)
Yes, it can.
For example:
TARGET=hoge
CC=gcc
.PHONY: compileandrun
compileandrun: $(TARGET)
./$(TARGET)
$(TARGET): $(TARGET).c
$(CC) -o $(TARGET) $(TARGET).c
If you have this Makefile and the source code hoge.c in your writable current directory, the program will be compiled and run with make command.
The other answers are good; however, you can use this other variant (a quick-and-dirty solution).
Suppose your makefile has this line to build your program:
NAME:
gcc source.c -o NAME
You can add your running command directly below:
NAME:
gcc source.c -o NAME
./NAME
This does the same as && concatenation: it runs the first command, and if successful, the second command too.
As long as you sequence your makefile dependencies correctly, there is nothing stopping you from running you C program from the makefile. That said, most people would expect a makefile to just build the file and not also run it at the same time.
I would recommend just writing the make command and the run command in a single line in the command line. This way you only need to press enter once but don't need to do anything fancy in the makefile itself. If your shell supports history completion, the next time you want to build and run again you only need to press "UP" and "ENTER"...
make && ./NAME
If you really want to put everything inside the makefile, I would recommend creating a separate "run" target in your makefile to reduce the chance of confusion, like in jdarthenay's answer.

C program, calling object file

I have written a simple helloworld.c program. I can compile it and run it on linux terminal using gcc and ./a.out command. My query is regarding calling .o file without any extension. For example, to run my program instead of typing "./helloworld.out", I want to run it using keyword "helloworld" on my terminal. Any hints???
Thank You.
Just compile using
gcc -o helloworld helloworld.c
The -o option is for the output file name
Then use:
./helloworld
You need the ./ to tell the shell where the executable resides, since the current directory is unlikely to be in $PATH.

Resources