How to rename/alias a function in C? - c

Say I have a library I am making and I want to call a function rename or puts, how can I keep the original rename and puts from stdlib or stdio or whatever, and yet have my own function be puts?
#include <stdio.h>
alias puts original_puts;
void
puts(char *c) {
original_puts(c);
}
How can I accomplish something to this effect?

You can't alias library functions, but you can alias your own using preprocessor directives.
For example:
mylib.h:
#include <stdio.h>
void my_puts(char *c);
#define puts(arg) my_puts(arg)
mylib.c:
void my_puts(char *c)
{
(puts)(c);
}
Now, anytime someone calls puts, it substitutes a call to my_puts instead. Also, when you want to call the "real" function in your wrapper, you can put the function name in quotes. Because the macro that does the substitution is a function-like macro, the parenthesis prevent the substitution from happening.

If compiling with gcc or clang, you can wrap the symbol with -Wl,--wrap:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int __real_puts(const char *c);
int __wrap_puts(const char *c) {
__real_puts("Hello");
__real_puts(c);
return 0;
}
int main(int argc, char const *argv[]) {
puts("world");
}
$ gcc src.c -Wl,--wrap=puts && ./a.out
Hello
world

Related

multiple definition in linking

I'm a beginner into Linking, lets say I have two .c files
file1.c is
#include <stdio.h>
#include "file2.c"
int main(int argc, char *argv[])
{
int a = function2();
printf("%d",a);
return 0;
}
and file2.c is
int function2()
{
return 2018;
}
when I compiled, there is a linker error which is multiple definition of function2, but I only define function once in file2.c?
You should create a header file, "file2.h", with:
int function2(void);
and a file "file2.c" with the function: "file2.h" with:
#include "file2.h"
int function2(void)
{
return 2018;
...
}
Then in your main you have to include the header with:
#include "file2.h"
Keep care that all those files should be in the same folder to avoid any link problem
Try something like this:
#include <stdio.h>
#include "file2.h"
int main(int argc, char *argv[])
{
int a = function2();
printf("%d",a);
return 0;
}
file2.h:
extern int function2(void);
and file2.c is
#include "file2.h"
int function2(void)
{
return 2018;
}
And then link it together.
The statement #include "file2.c" effectively incorporates the contents of file2.c into file1.c. Then file1.c is compiled as if it contains:
int function2()
{
return 2018;
}
Those lines define function2; they tell the compiler “Here is function2, create code for it.” Because those lines effectively appear in both file1.c and file2.c, your program has two copies of function2.
Instead, you should create file2.h that contains:
int function2();
That line tells the compiler “There exists a function called function2, but its definition is somewhere else.”
Then, in file1.c, use #include "file2.h" instead of #include "file2.c". This will tell the compiler, while file1.c is being compiled, what it needs to know to compile a call to function2. The compiler will have the declaration it needs, but it will not have the definition, which is not needed in file1.c.
Also, in file2.c, insert #include "file2.h". Then file2.c will contain both a declaration of function2 (from file2.h) and a definition of function2 (from the actual lines in file2.c). The purpose of this is so the compiler can see both the declaration and the definition while it is compiling file2.c, so it can warn you if there is a typographical error that makes them incompatible.
Additionally, in C, you should use int function2(void) rather than int function2(). For historic reasons, the latter leaves the parameters unspecified. Using (void) tells the compiler there are no parameters.

Why isn't my function invoked? I don't understand why a declaration is expected

Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);

Is it possible to redefine main function in c

I saw redefine function here using macro in c. So I am interesting is it possible to redefine main function?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
printf("Original main function\n");
return 0;
}
int _main(int argc, char **argv)
{
printf("New Original main function\n");
return main(argc, argv);
}
#ifdef DEBUG
#define main(argc, argv) _main(argc, argv)
#endif
Code compiled with out any problem but I am getting:
Original main function
So I am wondering why it does not work? When I use same techniques for malloc and free functions it works perfect. So what is wrong?
Why I want to do something like this? I want to do some code before main function will be executed. Is it possible in this way? if not is there are some other way?
P.S.: Sorry I did not mention in question. I am using gcc in Ubuntu OS. If you are down voting please give a reason in comments. You comments is very useful to my further development.
If you want to change entry point of your program, you don't need play with defines. You can use linker's -e option for that:
gcc -Wl,-e,__main ...
Please note extra underscore. Depending on some options, the symbol name can be different.
If your question is really: "can i execute code before main?" Then the answer is an emphatic YES.
Since you are using GCC, you can use function attributes (http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html) to mark a function as a constructor.
void pre_main_function (void) __attribute__ ((constructor));
A useful example can be found at http://www.geeksforgeeks.org/functions-that-are-executed-before-and-after-main-in-c/
EDIT
The following syntax can also be used:
__attribute__ (( constructor(n) ))
where n specifies the priority, allowing you to mark multiple functions to be executed before main whilst giving you control over the execution order ( the lower the value of n, the earlier the function is executed.
Your #define does not change the main function at all - it is a macro preprocessor.
The only effect of your #define will be to change the call to main in _main into a recursive call to _main(). But since _main is not called, this is dead code. This is what your code looks like after the preprocessor has run...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
printf("Original main function\n");
return 0;
}
int _main(int argc, char **argv)
{
printf("New Original main function\n");
return _main(argc, argv); /* recursive call due to macro replace */
}
This then leads to the next question - which is why redefine main at all? If you want some entirely different code to run on debug simply declare main as
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
#ifdef DEBUG
return debugApp( argc, argv);
#else
return productionApp( argc, argv);
#endif
}
N.B Just because you can do something doesn't mean you should do it. :-)

import function in c using extern keywod

I am having problem with importing external function to a main c file.
Here is my minimal code:
/* main.c */
#include<stdio.h>
extern int func()
int main(){
extern int func();
}
/*external file with one function that I want to
import*/
#include<stdio.h>
int func(){
printf("Hello World Again\n");
}
I compile and run like this - gcc main.c and then ./a.out but nothing is happening.
Any idea ?
You have to compile the file containing func also
gcc -Wall main.c external_file.c
(Note that the -Wall in the compiler command isn't absolutely necessary but is very good practice)
As noted by others, you also need to fix your code to call func rather than just re-declaring it.
Because you only declared the function, You never called it!
extern int func();
Declares a function. To call it you must have:
int main()
{
func();
}
You are just declaring again in main function..
you need to call the function to work..#include
extern int func()
int main(){
func();
}
/*external file with one function that I want to
import*/
#include<stdio.h>
int func(){
printf("Hello World Again\n");
}
Edits: question has changed.
extern is only used for external variables. You just need a prototype for the function.
#include <stdio.h>
void func(void); /* <-- prototype */
int main(int argc, char * argv[])
{
func();
return 0;
}
void func(void){
printf("Hello World Again\n");
}
Notice a few things. A prototype of int func() means no parameter checking in C - this is different to C++. Also, you are not returning anything from the function, so I replace it with void func(void)

Syntax error in C program

#include < stdio.h >
int main() {
char *s;
s=call();
printf(s);
}
char* call() {
return("hello");
}
Why these code not working. It's generating an error. How do I make it work?
Two things:
You can't put spaces inside the angle brackets when including a system header (e.g. #include <stdio.h>
You need a prototype for call()

Resources