I don't know what's wrong with my code....Its not giving expected output - c-preprocessor

Here I used #pragma directive which is used to call a function before and after main function in a C program.
So, my expected output is---> Hi
HELLO
BYE
But when executing this code I am getting output as---> HELLO
#include <stdio.h>
#pragma startup fun1
#pragma exit fun2
void fun1();
void fun2();
int main()
{
printf("\nHELLO");
return 0;
}
void fun1()
{
printf("\nHi");
}
void fun2()
{
printf("\nBYE");
}

From GCC's onlinedocs, 7 Pragmas.
The ‘#pragma’ directive is the method specified by the C standard for
providing additional information to the compiler, beyond what is
conveyed in the language itself. The forms of this directive (commonly
known as pragmas) specified by C standard are prefixed with STDC. A C
compiler is free to attach any meaning it likes to other pragmas. All
GNU-defined, supported pragmas have been given a GCC prefix.
That is to say, the startup and exit pragmas are not supported by GCC.
GCC does support using attributes to create similar behaviour. In particular:
__attribute__((constructor))
__attribute__((destructor))
__attribute__((constructor (PRIORITY)))
__attribute__((destructor (PRIORITY)))
For your example:
#include <stdio.h>
void fun1() __attribute__((constructor));
void fun2() __attribute__((destructor));
int main (void){
printf ("\nHELLO");
}
void fun1 (){
printf ("\nHI");
}
void fun2 (){
printf ("\nBYE\n");
}

Related

What's asm labels in C language?

When browsing glibc code, I found some code beyong my understanding of C language, it's introduced in this commit. Code is simplified as below.
#include <stdio.h>
int foo(void) {
printf("%s \n", __FUNCTION__);
return 0;
}
int bar(void) asm("foo");
int main(int argc, char *argv[]) {
bar();
return 0;
}
Output:
foo
What's asm labels in C language?
It does not exist in C programming language.
It's a GCC extension to the C language, that basically replaces the function name with another function name upon compilation.
This program:
void bar(void);
void func() { bar(); }
Compiles to:
func:
jmp bar
But this program:
void bar(void) asm("somename");
void func() { bar(); }
Compiles to:
func:
jmp somename
I believe, the idea of the commit is that GLIBC code that tests sqrt will not be optimized by the compiler, so that the test code can test the generic implementation not the built-in compiler implementation the compiler uses to optimize.

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)

Why #pragma is not working on ideone?

Consider the below program:
#pragma startup foo1
#pragma exit foo2
void foo1()
{
printf("Called before main\n");
}
void foo2()
{
printf("Called after main\n");
}
int main()
{
printf("main called\n");
return 0;
}
I am getting the output as: http://ideone.com/ooMFI
main called
Why the pragma is not working here?
Why foo1() & foo2() are not called?
Because none of these pragmas are recognized by GCC. In general, stay clear of pragmas if you're trying to write portable code, because they differ per compiler and even per platform within the same compiler family.

Can I re-define a function or check if it exists?

I have a question about (re-)defining functions. My goal is to have a script where I can choose to define a function or not.
Like this:
void func(){}
int main(){
if (func)func();
}
AND without the function, just:
int main(){
if (func)func();
}
Anybody an idea?
You can do this in GCC using its weak function attribute extension:
void func() __attribute__((weak)); // weak declaration must always be present
int main() {
if (func) func();
// ...
}
// optional definition:
void func() { ... }
This works even if func() is defined in another .c file or a library.
Something like this, I think. Haven't used function pointers much, so I may have gotten the syntax slightly wrong.
void func()
{
#define FUNC_PRESENT
// code
}
void (*funcptr)();
#ifdef FUNC_PRESENT
funcptr = func;
#else
funcptr = NULL;
#endif
int main()
{
if (funcptr)
funcptr();
}
Use function pointers, set them dynamically based on runtime conditions, and check for null pointers or wrap them in methods that do that check for you.
Only option in C I can think of.
In C++ you could combine templates and DLLs to dynamically define at runtime.
Really the only way that you can "choose to define a function or not" is with C preprocessor directives. For example:
#ifdef some_name
void func() {
do_whatever();
}
#else
//the else part is optional
#endif
To set these "variables" you use #define some_name
The trouble is, all of this needs to be done at compile time (before that, actually) so it can't be done with an if statement like in your example. If you want an if statement to control your program flow, just use it and don't bother with trying to rename functions or using function pointers or something.
Introduction
I guess that you are trying to do this:
Two modules, a.o and b.o
b.o contains a definition for void foo()
a.o calls void foo() only if b.o is also linked into the final executable.
This could be useful for a "plugin" system.
Variation 1
You can simulate it using function pointers. I don't know enough C to write this in proper C code, but pseudocode looks like this:
a.h
extern collectionOfFuncPtrs_t list;
int addFuncPtr();
a.c
#include "a.h"
collectionOfFuncPtrs_t list;
int addFuncPtr(FuncPtr p) {
- add func ptr to list
- return 0
}
int main() {
- loop through list of function pointers
- call functions through them
}
b.c
#include "a.h"
void bar() { /* ... */ }
static int dummy = addFuncPtr(&bar);
c.c
#include "a.h"
void ayb() { /* ... */ }
static int dummy = addFuncPtr(&ayb);
Conclusion
Now, you can link in b.o and/or c.o as you wish, and int main() will only call bar() and/or ayb() if they exist.
Variation 2
Experiment with variations on this theme if it looks like it may be useful to you. In particular, if you have only a specific number of conditionally-defined functions, you could use a bunch of individual function pointers rather than some list:
a.h
extern fptr_t bar_ptr, ayb_ptr;
a.c
#include "a.h"
int main() {
if (bar_ptr)
bar_ptr();
if (ayb_ptr)
ayb_ptr();
}
b.c
#include "a.h"
void bar() { /* ... */ }
fptr_t bar_ptr = &bar;
b_dummy.c
#include "a.h"
fptr_t bar_ptr = 0;
c.c
#include "a.h"
void ayb() { /* ... */ }
fptr_t ayb_ptr = &ayb;
c_dummy.c
#include "a.h"
fptr_t ayb_ptr = 0;
Conclusion
Now either link b.o or b_dummy.o; and either link c.o or c_dummy.o.
I hope you get the general idea, anyway...!
Bootnote
This is a lot easier in C++ where you can write a module registration system very easily with std::maps and constructors.
In C? Only by using the preprocessor as stated in other answers.
C isn't a dynamic language like, say, Python.
The right way to do what I think you're asking about in C is to use function pointers. You can take the address of a function, assign it to a variable, test it for nil, etc. However, plain old C isn't a very dynamic language; you might be better off using a different language.
if you don't mind compiler specific extension, you can use __if_exists:
#include <iostream>
using namespace std;
// uncomment the following, and it'll still work
void maybeFunc(){ cout << "running maybe" << endl; }
int main(){
cout << "hi!" << endl;
__if_exists(maybeFunc)
cout << "maybe exists!" << endl;
maybeFunc();
}
}
this works in msvc by default, and in clang if you use the -fms-extensions flag.

More than one __attribute__ in C with gcc

Can you add more than one attribute to an identifier in C with gcc?
Here is what I have now. I left out the include statements because they get scramble in the post.
If there is a way to add two, what is the general syntax, and how can I do it both with the defintion, and with a prototype? Thank you. :-)
main() {
printf("In Main\n");
}
__attribute__ ((constructor)) void beforeMain(void)
{
printf("Before Main\n");
}
There are two different ways of specifying multiple attributes in C with GCC:
#include <stdio.h>
// Attributes in prototypes:
__attribute__((constructor, weak)) void beforeMain(void);
__attribute__((constructor)) __attribute__((weak)) void beforeMain2(void);
int main(){
printf("In Main\n");
return 0;
}
// Attributes in definitions:
__attribute__((constructor, weak)) void beforeMain(void){
printf("Before Main 1\n");
}
__attribute__((constructor)) __attribute__((weak)) void beforeMain2(void){
printf("Before Main 2\n");
}
The code above compiles and runs correctly for me under gcc version 4.4.3.
You can use multiple __attribute__ specifiers separated by spaces.
char s[3] __attribute__((aligned(32))) __attribute__((weak));

Resources