Hello,
I had no idea which title I should have chosen. Anyway, I'll try to explain my problem.
What I'm trying to do is to prevent some code using functions in a header. This problem causes invulnerabilities in my application. I made a test program to show what exactly I'm trying to do. I think it's called macros, but I have no idea if what I'm doing is good, I read some tutorials on macros, but it didn't work.
I'm using Linux and GCC 4.2.4.

main.c

#define _MAIN_
#include "test.h"

int main(){
    fcn1();
    fcn2();

    return 0;
}

test.h

#ifdef _MAIN_
void fcn1();
#endif

#ifndef _MAIN_
void fcn2();
#endif

code.c

#include <stdio.h>
#include "test.h"

void fcn1(){
    printf("No problem, you can enter here.\n");
}

void fcn2(){
    printf("Who let you in here?!\n");
}

Commands:
gcc -c main.c
gcc -c code.c
gcc main.o code.o -o test

Output:
No problem, you can enter here.
Who let you in here?!

What I want to know:
- Are macros enough to solve this problem?
- Are there any mistakes in the way I use macros?
- What might be a solution to this problem? (if macros aren't enough to solve this problem)

Thanks for your help.
-Marek

Recommended Answers

All 3 Replies

I'm not sure what you are trying to accomplish, but that header file is really unnecessary. Both functions are coded in code.c and the header file will not prevent that. But you should get an error in main.c because fcn2() was not declared (because ifdef'ed out in the header file).

You mean like the keyword static, to keep it from external linkage; or volatile to keep the compiler from messing with it?
I'm thinking you're meaning static.

I'm not sure what you are trying to accomplish, but that header file is really unnecessary. Both functions are coded in code.c and the header file will not prevent that. But you should get an error in main.c because fcn2() was not declared (because ifdef'ed out in the header file).

Yes, I expected to get an error, but didn't get one.
What I'm trying to accomplish:
"What I'm trying to do is to prevent some code using functions in a header."
It may be lacking something like "(...)prevent some code using some of the functions(...)", but I thought it would be enough for an explanation.

You mean like the keyword static, to keep it from external linkage; or volatile to keep the compiler from messing with it?
I'm thinking you're meaning static.

Might be, I tried using it in the header and what I got was what I wanted to get: main.c:(.text+0x17): undefined reference to `fcn2' But I also got: test.h:3: warning: ‘fcn2’ used but never defined Which doesn't solve my problem.

Some further words of explanation:
My application actually calls function using the name of function in user's input. It's like a command prompt of sorts. So I expect to get that error when the application is running, not on compilation like the test application does. But it's like a "good error" :P

If the explanation is lacking something important please tell me what.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.