Executable path as a variable parameter to exec - c

I've an application which needs to call a specific program 'mips64-unknown-linux-gcc' for linking all objects from a script with all required args for linking.
I am writing an exec function to call the compiler passed by script along with it's args. For this I wrote the code:
//prog.c : gcc prog.c -o prog
int main(int argc, char *argv[]) {
execvp("mips64-unknown-linux-gcc",argv);
}
This works, but the mips64-unknown-linux-gcc and argv are variables from script input.
I need execv first argument to be a variable which is compiler to be invoked. I can somehow (maybe) retrieve it by getenv(”CC”) but due to other dependencies my requirement is that exec shall accept the compiler and args at runtime (something like below). Is there any way I can do this?
./prog mips64-unknown-linux-gcc --sysroot=<<...>> -O3 -Wl -L <<...>> -L <<...>> -I <<...>> -L <<...>> abcd.o a1.o b2.o -o prog
I described my problem at my best. Please ask if anything is not clear.

From your example command line it seems that you want to take the first argument from command line as your command to execute and everything else should be passed to that command.
That is basically the same command line execpt for the first argument.
This makes things rather easy.
Looking at argv you will find these string:
char *argv[] = {"proc","mips64-unkown-linux-gcc", "--sysroot=<<...>>", ..., "-o", "prog", NULL};`
You can use that and call your command:
execvp(argv[1], argv+1);
Of course you should check whether you have at least one argument.
If you want do filter some options and handle in your own program instead of blindly passing it to execvp you must rebuild your own array of arguments where you do not include those options.

Related

What does, " usage: ./a.out <integer value>" mean?

I have a code, and the error keeps saying usage: ./a.out <integer value>. I looked online for two days to see how to fix it, but I could not find any concrete answers. If someone could tell me what it means, and how I can fix it, that would be wonderful.
It means that when you run your program you will have run it in the command line with an argument:
./ program_name(a.out) integer_value
^^^^^^^^ ^^^^^^
program argument
Where program_name is the name you gave your program when you compiled it, if you do not provide one it's default name will be a.out for linux based systems and a.exe for Windows.
integer_value is an argument you must use, otherwise the condition if(argc != 2) will evaluate to true and the program will exit showing you that message, which is part of the program.
argc is part of the main function's arguments and it represents number of arguments, including the program name, you use when executing the program.
argv is also part of the main function's arguments and is an array of strings in which argv[0] is the program name, argv[1] is the actual argument (if you have more arguments it continues, argv[2], etc.).
To answer your question, the program is fine, it doesn't need fixing, it just needs to be correctly executed. Open a command window/terminal, navigate to the directory where the executable is located an execute it as explained.
BTW, the code should be posted as text, not images.
You can learn more about command line arguments in What does int argc, char *argv[] mean?

How to pass run time arguments to a function in c through a shell script

I have a shell script which has to take arguments from the command line and pass it to a function in C. I tried to search but didn't find understandable solutions. Kindly help me out.
Should the arguments be passed via an option as a command in the shell script?
I have a main function like this:
int main(int argc, char *argv[])
{
if(argc>1)
{
if(!strcmp(argv[1], "ABC"))
{
}
else if(!strcmp(argv[1], "XYZ"))
{
}
}
}
How to pass the parameters ABC/XYZ from the command line through a shell script which in turn uses a makefile to compile the code?
You cannot meaningfully compare strings with == which is a pointer equality test. You could use strcmp as something like argc>1 && !strcmp(argv[1], "XYZ"). The arguments of main have certain properties, see here.
BTW, main's argc is at least 1. So your test argc==0 is never true. Generally argv[0] is the program name.
However, if you use GNU glibc (e.g. on Linux), it provides several ways for parsing program arguments.
There are conventions and habits regarding program arguments, and you'll better follow them. POSIX specifies getopt(3), but on GNU systems, getopt_long is even more handy.
Be also aware that globbing is done by the shell on Unix-like systems. See glob(7).
(On Windows, things are different, and the command line might be parsed by some startup routine à la crt0)
In practice, you'll better use some system functions for parsing program arguments. Some libraries provide a way for that, e.g. GTK has gtk_init_with_args. Otherwise, if you have it, use getopt_long ...
Look also, for inspiration, into the source code of some free software program. You'll find many of them on github or elsewhere.
How to pass the parameters ABC/XYZ from the command line through a shell script
If you compile your C++ program into an executable, e.g. /some/path/to/yourexecutable, you just have to run a command like
/some/path/to/yourexecutable ABC
and if the directory /some/path/to/ containing yourexecutable is in your PATH variable, you can simply run yourexecutable ABC. How to set that PATH variable (which you can query using echo $PATH in your Unix shell) is a different question (you could edit some shell startup file, perhaps your $HOME/.bashrc, with a source code editor such as GNU emacs, vim, gedit, etc...; you could run some export PATH=.... command with an appropriate, colon-separated, sequence of directories).
which in turn uses a makefile to compile the code?
Then you should look into that Makefile and you'll know what is the executable file.
You are using and coding on/for Linux, so you should read something about Linux programming (e.g. ALP or something newer; see also intro(2) & syscalls(2)...) and you need to understand more about operating systems (so read Operating Systems: Three Easy Pieces).
See following simple example:
$ cat foo.c
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
for (i = 0; i < argc; ++i) {
printf("[%d] %s\n", i, argv[i]);
}
return 0;
}
$ gcc foo.c
$ ./a.out foo bar
[0] ./a.out
[1] foo
[2] bar
$

Command line arguments with makefile

Edit:
This is different than the other question because the variables that I put on the unix command line will be "p2 -s input.txt", where my main.c file will manipulate them.
So normally when working with command line arguments, my code would be something like:
int main(int argc, char argv[])
{
printf("%d", argc);
return 0;
}
How would I do this with a makefile?
GNU make is not C.
Passing arguments cannot be done the same way.
If you'd like to provide custom arguments to your makefile,
you may consider doing this through variable assignments.
for example, take the following makefile:
all:
#echo $(FOO)
It can be called via the command line like this:
make FOO="test"
and it will print test.
Other options to consider:
setting environment variables before calling make
relying on
different targets specified inside the Makefile
Use a makefile like this:
COMMAND = echo 'You did not specify make COMMAND="cmd 2 run" on the command line'
all:
${COMMAND}
Now when you run make COMMAND="p2 Hi", you will get that command run and the appropriate output. If you forget to specify COMMAND="something or another" then you get an appropriate memory jogger.

Handle command line arguments? [duplicate]

This question already has an answer here:
Pass File As Command Line Argument
(1 answer)
Closed 8 years ago.
So, I can make my program run and all, but if I'm given
$ ./a.out -f Text.txt
I'm just not sure how to get the program to make the connection that -f indicates a file. What is the logic for doing this?
The main function has signature int main(int argc, char**argv); so you can use the argc (which is positive) & argv arguments. The argv array is guaranteed to have argc+1 elements. The last is always NULL. The others are non-nil, non-aliased zero-byte terminated strings. Notice that often some shell is globbing the arguments before your program is started by execve(2): see glob(7).
For example, if you type (in a Linux terminal) myprog -i a*.c -o foo.txt and if at the moment you type that the shell has expanded (by globbing) a*.c into a1.c and a2.c (because these are the only files whose name start with a and have a .c suffix in the current directory), your myprog executable main program is called with
argc==6
argv[0] containing "myprog" (so you could test that strcmp(argv[0],"myprog") == 0)
argv[1] containing "-i"
argv[2] containing "a1.c"
argv[3] containing "a2.c"
argv[4] containing "-o"
argv[5] containing "foo.txt"
argv[6] being the NULL pointer
In addition you are guaranteed (by the kernel doing the execve(2)) that all the 6 argv pointers are distinct, non-aliasing, and non-overlapping.
GNU libc gives you several ways to parse these arguments: getopt & argp. getopt is standardized in POSIX (but GNU gives you also the very useful getopt_long(3))
I strongly suggest you to follow GNU conventions: accept at least --help and --version
The fact that e.g. -f is for some option and not a file name is often conventional (but see use of -- in program arguments). If you happen to really want a file named -f (which is a very bad idea), use ./-f
Several shells have autocompletion. You need to configure them for that (and you might configure them even for your own programs).

command line arguments into make file

I have a C file for which I want to give cmd line arguments.
Say
$ make --argument1
or something like this.
So that in my main program I should be able to do argv[1] and be able to access the variable.
I have tried looking for ways of doing this. Is there actually a way of doing this?
These were the relevant content I found on the GNULinux manual about make.
variables defined on the command line are passed to the sub-make
through MAKEFLAGS. Words in the value of MAKEFLAGS that contain ‘=’,
make treats as variable definitions just as if they appeared on the
command line.
Is this what I need to read up more or is this in a different context?
Do let me know.
I think you misunderstand the use of command line arguments - they are given when the executable is executed not when it is compiled.
Better example
foo.c
#include <stdio.h>
int main()
{
int myval=DEFVAL;
printf("myval=%d\n", myval);
return 0;
}
Makefile
DEFVAL=17
foo: foo.c
gcc -DDEFVAL=${DEFVAL} foo.c -o $#
Your question doesn't make a whole lot of sense, so I'm going to read between the lines and guess that you have a Makefile that someone else wrote, and when you run make, it runs some program, and that's the program that you want to give command line arguments.
In order to do that, you'll probably have to modify the Makefile. In order to that, it helps to understand how make works and how to use it (you might want to find a book on the subject, such as this one), but it may possible to modify the Makefile without too much trouble.
Somewhere in the Makefile, you'll find the action line that is used to invoke you're program. If you can find that line, you can add the argument you want.

Resources