I'm looking for an efficient (fast and secure) method to communicate multiple scripts (and their associated main function ()) to each other. A bit like the principle of the G-WAN project which uses a launcher (./gwan) to read / load / compile different .c files which each contain (or not) a main() function.
Ideally, my launcher should be able to execute the main () functions of other scripts while sharing information through their argv variables.
But as you know, gcc -Wall script1.c script2.c script3.c -o test return me an error of multiple definition of function main(), and gcc -Wl,--allow-multiple-definition -Wall script1.c script2.c script3.c -o test interprets only the first script1.c main() function.
Maybe the solution would be to have a first script (script1.c) which compiles the other scripts (script2.c and script3.c) via a shared variable?
Thanks for your help and sorry for my limited english.
script1.c:
int main(int argc, char *argv[]){
...
int i = main(argc, argv); // main for script2.c
if(i == 0)
main(argc, argv); // main for script3.c
...
return(0);
}
script2.c:
int main(int argc, char *argv[]){
...
return(0);
}
script3.c:
int main(int argc, char *argv[]){
...
return(0);
}
You can't have multiple main function in c, it's simply not allowed, what you can do is have your main function in your first .c file, and whatever you want in your others .c files.
Then you can write :
#include "script2.c"
#include "script3.c"
on top of your script1.c file.
If you want to compute values inside a file and "send" it to main file, you can do it through functions, defined in your other files, since you include it !
Notice that there is no scripts in C !
Related
I searched dirent to find C routines
find / -iregex ".*/dirent.h$" 2>/dev/null
Which return series of identical ones
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/dirent.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/dirent.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/dirent.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/dirent.h
How could I ensure which dirent.h is the one my program invoke?
If you get the compiler to show you the code after pre-processing, you should be able to work it out.
So, for example, with gcc, you can write a nominal program.c:
#include <dirent.h>
main(int argc, char **argv){
return 0;
}
And then run:
gcc -E program.c
and study what it includes.
Related useful info here.
The code is like (real noob question) :
int main(int argc, char **argv){
//some code
}
I know, it means I have to give some arguments while executing in the terminal, but the code does not require any arguments or information from the user. I don't know what to give as the argument?
For example:
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
}
Compile with GCC,
$gcc prog.c -o prog
$./prog
Hello World
So, If you do not use agave in your code, then, there is no need to provide an argument.
I have to give some arguments while executing in the terminal,
No, you don't have to. You may give some arguments. There are conventions regarding program arguments (but these are just conventions, not requirements).
It is perfectly possible to write some C code with a main without argument, or with ignored arguments. Then you'll compile your program into some executable myprog and you just type ./myprog (or even just myprog if your PATH variable mentions at the right place the directory containing your myprog) in your terminal.
The C11 standard n1570 specifies in ยง5.1.2.2.1 [Program startup] that
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent) or in some other implementation-defined manner.
The POSIX standard specifies further the relation between the command line, the execve function, and the main of your program. See also this.
In practice I strongly recommend, in any serious program running on a POSIX system, to give two argc & argv arguments to main and to parse them following established conventions (In particular, I hate serious programs not understanding --help and --version).
You can always pass some number of arguments or pass nothing unless you are checking for the number of arguments and arguments passed (or forcing compiler to do so). Your command interpreter has no idea what your program is going to do with the passed argument or whether the program need any argument. It's your program which takes care of all these things.
For example,
int main(void){
return 0;
}
you can pass any number of arguments to the above program
$ gcc hello.c -o hello
$ ./hello blah blah blah
In case of
int main(int argc, char **argv){
return 0;
}
you can pass no arguments.
$ gcc hello.c -o hello
$ ./hello
For
int main(int argc, char **argv){
if(argc < 3){
printf("You need to pass two arguments to print those on the terminal\n");
exit(0);
}
else{
printf("%s %s\n", argv[1], arv[2]);
}
return 0;
}
You have to pass two arguments because the program checking the number of arguments passed and using them
$ gcc hello.c -o hello
$ ./hello Hello world
I want to use the C-coder in Matlab. This translates an m-code to C-code.
I use a simple function that adds 5 numbers.
When the code is generated there are a lot of C- and H-files.
of course you could just pick the code you need and import it in your code, but that's not the point of this exercise, as this will no longer be possible when the matlab-code will get more difficult.
Matlab delivers a main.c file and a .mk file.
/* Include Files */
#include "rt_nonfinite.h"
#include "som.h"
#include "main.h"
#include "som_terminate.h"
#include "som_initialize.h"
//Declare all the functions
int main(int argc, const char * const argv[]){
(void)argc;
(void)argv;
float x1=10;
float x2=20;
float x3=30;
float x4=40;
float x5=50;
float result;
/* Initialize the application.
You do not need to do this more than one time. */
som_initialize();
main_som();
result=som(x1,x2,x3,x4,x5);
printf("%f", result);
som_terminate();
return 0;
}
When I run this on a raspberry-pi with
gcc -o test1 main.c
It gives me undefined references to all the functions...
Any ideas what went wrong?
You have to build it with the generated makefile (the mk file) so it links with the correct Matlab libraries - that's where those functions are defined:
$ make -f test.mk
You also need to compile the other C files along with your main.c. If main.c is in the same directory as the generated code, you should be able to just do:
gcc -o test1 *.c
If the generated code is in another directory, then you can do something like:
gcc -o test1 /path/to/code/*.c -I/path/to/code main.c
Let's say I have the following programs:
//gcc a_program.c -o a_program
void foo() {
printf("foo called\n");
}
int main(int argc, char** argv) {
execvp("some_other_program", argv);
}
//gcc some_other_program.c -o some_other_program
int main(int argc, char** argv) {
foo();
}
Is there a way to call foo from the new process image that execvp() creates? dlsym() only works on binaries loaded as dynamic libraries - according to the man page, at least - so I can't just search for the symbol in the new process, even if it exists.
No. When you exec a program the current process is completely replaced by the new one. Environmental factors like open file descriptors are retained, but code isn't. You can't call functions from the old process in the new one.
I am building a user defined shell where the shell dynamically links libraries
I have the following snippet from the main file that contains the global variable declarations...
char *prompt = "upsh";
int main()
{ ...
then I have a shared library as follows...
extern char *prompt;
int setprompt(char *argv[]) {
prompt = argv[1];
return 0;
}
my problem is that when I link the library from the main program I get the error
./setprompt.so: undefined symbol: prompt
...maybe this is a compilation issue?
As Naveem Kumar commented, you should provide compilation steps. Does the following reproduce what you meant? This worked in my laptop.
Makefile
all: main
libsetprompt.so: setprompt.c
gcc -fPIC -DPIC -shared setprompt.c -o libsetprompt.so
main: main.c libsetprompt.so
gcc main.c -o main -L. -lsetprompt
clean:
rm main libsetprompt.so
main.c
char *prompt = "upsh";
int main(int argc, char *argv[])
{ return 0; }
setprompt.c
extern char *prompt;
int setprompt(char *argv[])
{
prompt = argv[1];
return 0;
}
I do not think it is a comilation issue. If i get it right, you have a console application, which means that argc and argv must be passed to the main function in order to use them, but they are not passed. I guess your problem is that you call int setprompt(char *argv[]) without actually passing argv[]. But i may be wrong, more code would help to tell for sure.
But probably instead of int main() there should be int main(int argc, char **argv)