943,748 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 590
  • C RSS
Jan 23rd, 2009
0

Preventing code from using code

Expand Post »
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
  1. #define _MAIN_
  2. #include "test.h"
  3.  
  4. int main(){
  5. fcn1();
  6. fcn2();
  7.  
  8. return 0;
  9. }

test.h
  1. #ifdef _MAIN_
  2. void fcn1();
  3. #endif
  4.  
  5. #ifndef _MAIN_
  6. void fcn2();
  7. #endif

code.c
  1. #include <stdio.h>
  2. #include "test.h"
  3.  
  4. void fcn1(){
  5. printf("No problem, you can enter here.\n");
  6. }
  7.  
  8. void fcn2(){
  9. printf("Who let you in here?!\n");
  10. }

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
Last edited by venomxxl; Jan 23rd, 2009 at 7:38 pm.
Similar Threads
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009
Jan 23rd, 2009
0

Re: Preventing code from using code

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).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jan 23rd, 2009
0

Re: Preventing code from using code

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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 24th, 2009
0

Re: Preventing code from using code

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.

Quote originally posted by MosaicFuneral ...
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"

If the explanation is lacking something important please tell me what.
Last edited by venomxxl; Jan 24th, 2009 at 7:07 am.
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: linking of .c and .h files
Next Thread in C Forum Timeline: How to clear the character in the file???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC