Change the entry point "main" [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change the entry point "main" of my program ?
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%s\n","Hello world!");
return 0;
}

gcc -o entry_test -Wl,-eother entry_test.c
#include<stdio.h>
int other(void){//can't use argc, argv
printf("Bye-Bye world!\n");
return 0;
}
int main(int argc, char *argv[]){
printf("%s\n","Hello world!");
return 0;
}

If you're using gcc, I found a thread that said you can use the -e command-line parameter to specify a different entry point; as BLUEPIXY stated
see also :
Avoiding the main (entry point) in a C program
-see the following link for more details about "-e" option :
http://gcc.gnu.org/ml/gcc/2001-06/msg01959.html
Another way is to change the starting function is in the linker "start up" file.. linker may include some pre-main startup code in a file like start.o and it is this piece of code which runs to set up the C environment then call main (as in all embedded tool-chains). There's nothing to stop you replacing that with something that calls another function instead.
here is a terrific explanation for startup files:
What is the bootloader and startup code in embedded systems?
I voted this question up as it really may be useful for some embedded c developers.

Related

Different Executables from the same source file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Let's say I am creating a program and a makefile, and I want to be able to create three different executables with different names that all do different things, all with the same source file. Is this at all possible?
Option 1
Write a program that examines argv[0] to see what name it has been executed with and branches based on that:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
size_t length = strlen(argv[0]);
if (3 <= length && strcmp(argv[0]+length-3, "foo") == 0)
printf("This is the foo program. It does foo things.\n");
else if (3 <= length && strcmp(argv[0]+length-3, "bar") == 0)
printf("This is the bar program. It does bar things.\n");
else
printf("This is the default program. It does default things.\n");
}
Compile the program and call the executable x or some other name.
Presuming you are using some version of Unix, link the file to more names:
ln x foo
ln x bar
Run the program with various names:
% ./x
This is the default program. It does default things.
% ./foo
This is the foo program. It does foo things.
% ./bar
This is the bar program. It does bar things.
Option 2
Use preprocessor symbols to build different programs. The source code can be:
#if Option == 1
#include "Program1.c"
#elif Option == 2
#include "Program2.c"
#else
#include "ProgramDefault.c"
#endif
The source code does not have to be in separate files. The above is only an example, and all of the source code could be in the file directly instead of included with #include.
With GCC and Clang, you can define a preprocessor symbol with -DOption=value on the command line to compile the program. The rules in a makefile can build the different programs by using different values in the compile command.

How to write a C/C++ wrapper program in Linux that is fully transparent [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Note: This is not a question to ask for a program, It asks about some tech details, see the question bellow first.
I need to write a wrapper program in C/C++ for an existing program. I know we need to use exec/fork/system and pass through the parameters then return the result of the program.
The question is, how to ensure that both the invoker program(that invoke the wrapper) and the wrapped program work exactly like before (ignore timing differences). There maybe subtle things like environment parameters to deal with. fork/system/exec, which to use? Are they enough? Are there other factors to consider?
Let's say you have the following original program:
foo.sh
#!/bin/bash
echo "Called with: ${#}"
exit 23
Make it executable:
$ chmod +x foo.sh
Now the wrapper in C:
wrapper.c
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
printf("Executing wrapper code\n");
/* do something ... */
printf("Executing original program\n");
if(execv("./foo.sh", argv) == -1) {
printf("Failed to execute original program: %s\n", strerror(errno));
return -1;
}
}
Run it:
$ gcc wrapper.c
$ ./a.out --foo -b "ar"
Executing wrapper code
Executing original program
Called with: --foo -b ar
$ echo $?
23

addr2line print "??:0" even i use `gcc -g` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my code:
/* backtrace_foo1.c */
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define BACKTRACE() \
do {\
void *array[20];\
size_t size;\
char **strings;\
size_t i;\
size = backtrace(array, 20);\
strings = backtrace_symbols(array, size);\
for (i = 0; i < size; i++) {\
printf ("%s\n", strings[i]);\
}\
free (strings);\
} while(0)
void func1()
{
BACKTRACE();
}
void func()
{
func1();
}
int main(int argc, char **argv)
{
func();
return 0;
}
I compiled it by gcc -g -rdynamic and got
./a.out(func1+0x1f) [0x400905]
./a.out(func+0xe) [0x40097a]
./a.out(main+0x19) [0x400996]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x318ae1ecdd]
./a.out() [0x4007f9]
Then i use addr2line -e ./a.out -f 0x4007f9, i got
_start
??:0
This is my platform
gcc version 5.3.0 (GCC)
Linux 3.10.0_1-0-0-8
I shouldn't really answer this, since you don't really have a question or a stated problem. But sometimes I'm feeling nice...
While the code you write will start execution with the main function, the actual starting point is somewhere before that. There is startup code that will initialize the stdio system (stdin, stdout etc.) and initialize other things. This startup code then calls your main function like any other function.
The "problem" is that the startup code is not really part of your code, it's often a precompiled object file that the frontend program links your program with. And that object file probably doesn't have any kind of debug information, so you can't get any location information about it.

Weird behavior when passing * as a command line parameter on linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have this very simple c program:
#include <stdio.h>
int main (int argc, char ** argv){
printf ("%s\n",argv[1]);
}
When running it on Linux/bash like so:
./a.out *
I get the following output:
a.c
why?
Because * is a glob character that expands to the list of files in the current directory.
If you want to pass a literal * you will need to quote or escape it:
./a.out '*'
I didn't know that, but when running a command line that has a glob character , such as * or ?, the command line interpreter first expands the character and only then run the program.
For example, if your program is:
#include <stdio.h>
int main (int argc, char ** argv){
int i;
printf ("argc=%d\n",argc);
for (i=0;i<argc;i++){
printf("%d: %s\n",i,argv[i]);
}
}
and you run it like so:
./a.out *
, then the output will be:
argc=4
0: ./a.out
1: a.c
2: a.c~
3: a.out
Of course, the output will depend on the content of the current directory.

libyahoo Segmentation fault [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wrote this code:
#include <libyahoo2/yahoo2.h>
#include <libyahoo2/yahoo2_callbacks.h>
int main() {
int id ;
char username[255] = "slam";
char password[255] = "ss" ;
id = yahoo_init(username, password);
enum yahoo_status mYahoo ;
mYahoo = YAHOO_STATUS_AVAILABLE ;
yahoo_login(id , mYahoo );
return 0;
}
Compile it, gcc -l yahoo2 y.c -o yahoo and run it with ./yahoo gives me a error: Segmentation fault
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x001379b1 in yahoo_login (id=1, initial=0) at libyahoo2.c:1735
line 1735 code is:
tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
host, yss->pager_port, yahoo_connected, ccd, 0);
and see this :
(gdb) list YAHOO_CALLBACK
Function "YAHOO_CALLBACK" not defined.
How do I debug this?
How do I debug this?
Execute these commands:
(gdb) print yd->client_id
(gdb) print yss->pager_port
My guess would be that one or both of the above commands will fail, because yd or yss is NULL.
If so, the problem is earlier in libyahoo2.c, and it (apparently) doesn't check for errors properly.
The reason you can't list YAHOO_CALLBACK is most likely that it is a macro. Look in libyahoo2/yahoo2_callbacks.h -- it's very likely to be defined there.
Also, your link command line:
gcc -l yahoo2 y.c -o yahoo
is totally bogus. Correct command line should be:
gcc y.c -o yahoo -lyahoo2
You may want to read this explanation to understand why order of sources and libraries on command line matters.

Resources