Preventing code from using code

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Preventing code from using code

 
0
  #1
Jan 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Preventing code from using code

 
0
  #2
Jan 23rd, 2009
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).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Preventing code from using code

 
0
  #3
Jan 23rd, 2009
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Re: Preventing code from using code

 
0
  #4
Jan 24th, 2009
Originally Posted by Ancient Dragon View Post
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.

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC