Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
In foo.c
void location(char *path)
{
//to do
}
In main.c
int main()
{
char foopath[256];
location(foopath);
printf("%s\n",foopath);
}
Maybe it will show /lib/foo.so
I think I can use shell script such as ldd to get the path, but it seems not pretty.
I want to read a file at the same location as the foo.so. So I need the correct path.
You can use the "dl" library. Example of program which displays the name of the dynamic library file of the "fopen" symbol:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
int rc;
Dl_info info;
rc = dladdr(fopen, &info);
if (rc) {
printf("%s\n", info.dli_fname);
return 0;
}
return 1;
}
$ gcc example.c -l dl
$ ./a.out
/lib/x86_64-linux-gnu/libc.so.6
Related
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Some books claim that they used ansi c and use turbo c compiler to run these example. i tried to run these on linux but I found that these example are only for windows.
#include<stdio.h>
#include<conio.h>
/* #include<dos.h> */
int main()
{
int a;
clrscr();
scanf("%d", &a);
printf("%d",a);
getch();
return 0;
}
Can I call the above example ansi c? why or why not?
As #milevyo said, those functions are implemented by Borland's compilers. On Windows you can replace clrscr() with system("cls") and getch(); with _getch(); or, even better, getchar().
conio.h file directory is only supported by Borland C.You can use getchar() instead of getch().
If you have to use getch() anyhow then You can use curses.h file instead of conio.h. It gives mostly all the functionality of conio.h with getch().
If you don't get curses.h directory installed then you can download from here
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr(); /* start the curses mode */
int a;
scanf("%d", &a);
printf("%d",a);
getch();
endwin();
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Using gcc I can remove comments and unwanted blank lines, but I want to reduce a size of file also, is there any options in gcc or any other tool to do so
At present I do like this
gcc -fpreprocessed -dD -E -P source_code.c > source_code_comments_removed.c
Here is scenario assume that this is my source_code.c
#include <stdio.h>
main()
{
// declar variable i
int i=0;
/* multiline comment
for loop
demo stuff
*/
for(i=1; i<=5; i++)
{
// just print something
printf("Hello %d \n",i);
}
}
I want to minify like this, removed comments and blank lines
#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}
Note : I am on Linux please don't suggest any windows based solution
sed -rb 's/ {6}//g' main.c |
sed -rb 's/\/\/.*$//g' |
tr -d '\n' |
sed -rb 's/\/\*.*\*\///g' |
sed -rb 's/(#include.*>)/\1\n/g'
will give you:
#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}
However, as stated in the in the comments, this doesn't make much sense and will not reduce the size of your compiled object file!
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.