BPsite Forums

BPSITE => Geek's Corner => Topic started by: smi256 on February 02, 2007, 08:04:26 AM



Title: UNIX
Post by: smi256 on February 02, 2007, 08:04:26 AM
I, for the of me, can't write my own Makefile.
What it is that I want to do is have three files:
main.c - which will #include "functions.h"
functions.c - which will have functions in it, lets call them: foo() and bar()
functions.h - which has the declaration of foo()

The thing is that I want main.c to be able to call foo(), but not bar().

***** main.c *****
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"

main()
{  
   foo();
}//end main


***** functions.h *****
void foo(void);


***** functions.c *****
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"

void foo(void)
{  
   bar();
}//end foo

void bar(void)
{  
   printf("\nMeow?\n\n");
}//end bar



This is what I have for a make file:
***** Makefile *****
TEST2.exe : main.o functions.o
   cc main.o functions.o -o TEST2.exe

main.o : main.c
   cc -c main.c

functions.o : functions.c functions.h
   cc -c functions.c


The problem that I have is that with this setup, I can still call bar() from my main.c file...  :(   I don't even know if I'm making my Makefile correctly.  :unsure:
What do I do? :cry:


Title: UNIX
Post by: Rug on February 02, 2007, 01:00:55 PM
The problem is that you're using Unix, you snob. Linux is good enough for most people who like money :P.


Title: UNIX
Post by: Hornet on February 02, 2007, 06:56:18 PM
Not a clue about C, sorry to say.

In PHP, which is quite similar, one way around it would be to have bar() inside another function, and call that within the file that you want to be able to access bar().

Why on earth would you not want it to be able to reach a particular function, though?  If you don't want it, don't call it, and if you want something with the same name, just change it's name and use that instead.

Code:
function One () {
     print 'One<br />';
}

function Two () {
     function TwoA () {
          print 'TwoA<br />';
     }
}

One();
Two(); //don't call Two() if you want to hide TwoA
TwoA(); //Depending on how C handles things, you might want to wrap this in an 'if function exists' type clause.


Title: UNIX
Post by: SS on February 02, 2007, 09:49:17 PM
My C is rusty, but don't you just do:


public void alice()
{
 bob();
}

private void bob()
{
 printf("Woof!");
}



Or something along those lines anyway.


Title: UNIX
Post by: smi256 on February 03, 2007, 02:46:08 AM
Quote
The problem is that you're using Unix, you snob. Linux is good enough for most people who like money :P.
I agree, But we are forced to use UNIX at UCSC.

Hornet and SS,
I know those two methods work in Java, but I don't think they are in line with ANCII C

We're using cc on the UNIX systems to compile our programs :-( which means I can't sneak in any C++ is I wanted any OOP functionality.
I'll try anyway in spite of my thoughts.  I'm also going to try using static to hide my private functions.