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.
Related
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
I'm trying to clean up my project, to help expand it further, but there's about 200 lines of variables definitions, calculations and writing into arrays before starting the actual application, and it's an absolute mess.
Is there a way to put all of this into another file, and include these definitions in the file I'd like to use it in (and only that file, to avoid conflicts) ?
I tried creating something like "levelVars.c" and including it in the "level.c" file, but all I get is a bunch of errors.
There's also some custom types and SDL types in here, so..it might cause problems.
The reason I want to do all this is to clean up the file : I'm having trouble navigating between everything with such a massive block of variables.
I also can't reduce their numbers, as I need them all ; every variable is taken in by some functions and used by others, so I can't just reduce their scope and clean up this way. Well, I could maybe cut down ten variables like this, but it won't help much.
The beginning looks like this :
int trackSomething = 0;
int trackSomethingElse = 0;
int yetAnotherCount = 0;
bool active = false;
bool andAnother = false;
bool iThinkYouGotIt = false;
int arr[SIZE_1][SIZE_2];
for(int i = 0 ; i < SIZE1 ; i++)
{
for(int j = 0 ; j < SIZE2 ; j++)
{
arr[i][j] = 0;
}
}
....
while(active)
{
// The actual loop that does something meaningful with all this
}
Don't use the pre-processor to do includes. Use the linker:
$ cat variables.h
extern int d;
$ cat variables.c
int d = 57;
$ cat main.c
#include "variables.h"
#include <stdio.h>
int main(void) { printf("d = %d\n", d); return 0; }
$ gcc -c main.c
$ gcc -c variables.c
$ gcc main.o variables.o
$ ./a.out
d = 57
Your request that the variables only be available to one translation unit is somewhat difficult to enforce, and you really shouldn't try. (Ab)using the pre-processor to include the variable definitions with a #include to force the definitions and their usage to all be in the same translation unit will do it, but your code will be better organized if you don't do that.
I don't think that you'll achieve what you are looking for. You can define a variable in levelVars.c:
int count;
And you want to use it later in another file, don't you? Well, to do that you must reference it again in that new file but saying that the variable "comes from another file" by using extern:
extern int count;
So in the end you will end up with the same 200 variables in your file, only with extern in front of them...
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
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 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.
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 am using Mac OSX 10.8.4 and programming in C. I trying to use openmp and I am compiling with gcc-mp-4.7. I am working in bash. Currently I have an executable (I will call executable1 in the program) which I am trying to run in parallel by using a system call inside of an openmp parallel for loop. The example code is as follows:
my_omp.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
void main() {
int n = 100;
double var1 = 65.4;
char place[100] = "/under/a/rock";
double var2 = 4.5e4;
double var3;
char program[200];
int i;
#pragma omp parallel for private(program,var3)
for (i=0; i<=n; i++) {
var3 = var1*pow(var2,i);
sprintf(program,"./executable1 %.15e %s %.15e %d", var1, place, var2, var3, i);
printf("%s \n", program);
system(program);
}
}
I compile the program using gcc-mp-4.7 -fopenmp my_omp.c, then run the newly compiled executable, (differently named than exectuable1).
What seems to happen is that 8 (which I believe is the number of "cpus" openmp thinks I have) of the print statements will appear in the stdout (terminal) and then it will run only a single call of the executable1, then when it finishes it prints out another of the printf program lines, then runs another executable1 until it finishes the for loop (I know this because executable1 is extremely verbose, and it would be obvious is two where running as numbers printed to stdout would be out of synch and appearing in at multiples).
So it seems maybe that the printf is running in parallel, but for some reason the system() command is not? Does anyone have any ideas?
Thanks for any help you can offer.
UPDATE:
I have gotten this exact code to run properly on a lunix distribution with a different compiler, I will look into finding a better compiler to use in Mac OSX and see if that works.
The system(3) library call in OS X is implemented using a global mutex lock - see the system.c file in the source code of OS X's C library:
#if __DARWIN_UNIX03
pthread_mutex_lock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
...
#if __DARWIN_UNIX03
pthread_mutex_unlock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
Therefore when one thread calls into system(3), all other threads have to wait for the first call to finish, resulting in serialised execution.