GCC compiler in Ubuntu compiles nested functions [duplicate] - c

This question already has answers here:
Are nested functions a bad thing in gcc ? [closed]
(11 answers)
Implementation of nested functions
(1 answer)
Nested function in C
(9 answers)
Closed 3 years ago.
I wrote a small program in C on Ubuntu having function definitions within the main function and compiled it using GCC. It compiled and ran without error. However, writing the same program in Codeblocks on Windows doesn't compile, and issues an error that says function-definition is not allowed here.
So, does GCC on Ubuntu allow function definitions within a function? Will it always run perfectly (given that it has compiled and there is no logical error in the program)?

Related

Is there a way to retrieve the full assembly execution trace of a C program? [duplicate]

This question already has answers here:
Gdb process record/replay execution log
(1 answer)
How to run record instruction-history and function-call-history in GDB?
(2 answers)
Displaying each assembly instruction executed in gdb
(3 answers)
How to log CPU instructions executed by program with x64dbg?
(1 answer)
GDB: How to get execution history
(2 answers)
Closed 2 years ago.
I have a couple C programs that I basically want to disassemble. I was using objdump, but I realized that is static disassembly. Is there anyway to get the full assembly-level trace of the program of the instructions executed in order? I am running on x86-64.
Is there a way to retrieve the full stack trace/execution trace of a C program at the assembly level?
The call stack might not even exist. Some C implementations could (in some simple cases) inline every function call and work in registers (but Rice's theorem shows that this is not always possible). That might happen with a recent GCC doing whole-program link-time optimizations (e.g. invoked with gcc -O3 -flto -fwhole-program for both compilation and linking steps)
However, if you use Linux/x86-64 and if you want to retrieve at runtime the call stack, consider using Ian Taylor's libbacktrace. It is part of some recent GCC compilers.
Is there anyway to get the full assembly-level trace of the program of the instructions executed in order?
Alternatively, use and/or patch some processor emulator like Qemu.

Nested Functions in C [duplicate]

This question already has answers here:
Nested function in C
(9 answers)
Closed 3 years ago.
I read that nested function don't exist in C.
I then successfully executed the following piece of code using the cc command on my linux machine.
#include <stdio.h>
#include <string.h>
int main(){
float dummy(){
printf("hello\n");
}
dummy();
return 0;
}
I got the the output hello.
How does that play out?
Does my compiler support nested functions?
GNU C supports nested functions as an extension. Your cc binary on your Linux machine is almost certainly a symlink to gcc.
Nested functions don't exist in standard C. However, it might be supported as an extension in certain compilers like GNU.

Intercepting Arithmetic Operations in C program [duplicate]

This question already has answers here:
Operator overloading in C
(7 answers)
How do you get assembler output from C/C++ source in GCC?
(17 answers)
Is it possible to overload operators in C?
(4 answers)
Closed 4 years ago.
Is there a way that we can call user defined function when we are calling arithmetic operator in a C program just like operator overloading in C++. using GNU GCC Compiler?
Simply,
I have a function add(), and in my C program I have arithmetic Operation
c = a + b;
when I compile the program, it should call my add() function internally for + operator.
and Is there a way we can see what is the code that gcc compiler is calling when it encounters + operator?
No.
C does not work that way, you cannot overload/override the basic built-in operators.
Seeing the code is of course possible, either by making gcc emit it directly using -S, or by disassembling the resulting binary. The related binutils tool is objdump.
These days much such exploration can also be made "online" using the fantastic Compiler Explorer tools at godbolt.org, of course.

Problems with header in C [duplicate]

This question already has answers here:
Getting undefined reference to 'clock_gettime' error, for curl program
(2 answers)
Closed 5 years ago.
I have this piece of c code that I am in charge of speeding up. The code is put onto a RasPi and compiled. Two years ago the code was put on and compiled and it works. Now when I try to compile the same file it says there's an undefined reference to 'clock_gettime'. I looked it up and that function is defined in time.h. I thought that maybe that header wasn't installed or called in the code. At the beginning of the code it does say #include so that's not the problem. I checked if the time.h header was installed on the RasPi and it was there with the other headers. I opened it up with nano and the clock_gettime function was defined, so that's not the problem. What should I do? How do I solve this problem?
clock_gettime(2):
#include <time.h>
...
Link with -lrt (only for glibc versions before 2.17).

g++ ubuntu multithreading undefined reference [duplicate]

This question already has answers here:
Undefined reference to pthread_create in Linux
(16 answers)
Closed 7 years ago.
I am writing a c code on ubuntu that creates a certain number of threads
I have already added pthreads library but when I run the code
it ends up with this error
Threads.cc:(.text+0x128): undefined reference to 'pthread_create'
Threads.cc:(.text+0x15b): undefined reference to 'pthread_join'
I am using ubuntu 15.04 virtual machine.
I have tried many suggested solutions, non of them worked!
any help would be appreciated
I have already added pthreads library [..]
No. You haven't. If you did you wouldn't get this problem. You probably mean to say you included <pthread.h>. But that doesn't link with pthreads library.
Add pthread at the end of your compilation options. For example,
gcc -o out myfile.c -pthread

Resources