Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,263 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,496 people online right now. Registration is fast and FREE... Join Now!




External functions

 
Reply to this topicStart new topic

External functions, calling them

chris.tkd
2 Dec, 2006 - 03:31 AM
Post #1

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 60


Dream Kudos: 25
My Contributions
Im having a little trouble calling an external print function.

The external print function must take no args and must not have a return value.

The program must accept ints as input, copy the value to a global var called gM then print it using the external function until a 0 is input.

I get gM not found when i try to compile them.

I use the line cl /c main.c compolent.c to get .obj files, this is where it throws the error

and cl main.obj compolent.obj to link them.

How can i fix my globar var error?

My main source is:
CODE

#include <stdio.h>

long gM=1;

int main(void)
{
    while(gM!=0)
    {
        printf("\nEnter a list of ints");
        scanf("%ld",&gM);
        myPrintFunction();
    }
    return(0);
}


and my external function is
CODE

void myPrintFunction()
{
    printf("\nPrintFunction: %ld", gM);
}


Thanks for your help.
User is offlineProfile CardPM
+Quote Post

horace
RE: External Functions
2 Dec, 2006 - 03:58 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
if myPrintFunction() is defined in a different file to where variable gM is defined it needs to know its type and name, e.g. using extern
CODE

extern long gM;    // external variable

void myPrintFunction()
{
    printf("\nPrintFunction: %ld", gM);
}

here we declare variable gM as extern so memory is not allocated for it, extern means that the variable already exits external to the file, i.e. defined in another file.

This post has been edited by horace: 2 Dec, 2006 - 03:59 AM
User is offlineProfile CardPM
+Quote Post

Xing
RE: External Functions
2 Dec, 2006 - 04:03 AM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Try adding line extern long gM; before myPrintFunction. See if it works or not.
User is offlineProfile CardPM
+Quote Post

chris.tkd
RE: External Functions
2 Dec, 2006 - 07:58 AM
Post #4

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 60


Dream Kudos: 25
My Contributions
thanks fellas its working brilliant now!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month