www.digitalmars.com         C & C++   DMDScript  

c++ - Help needed!

reply xyk <xyk_member pathlink.com> writes:
Have been trying to figure out how to put a function in a separate file and make
it work.

Here is the main program, in a file named "tmpmain.c":

#include "myPrint.c"

void myPrintf();

int main()
{
int i=1;
myPrintf();
return 0;
}


Here is the function myPrintf(), in a file named "myPrintf.c":
void myPrint()
{
printf("ok!");
}


This is the error message I got when trying to compile under the DOS command of
Win XP:

E:\bjs\C>dmc tmpmain
link tmpmain,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

tmpmain.obj(tmpmain)
Error 42: Symbol Undefined _myPrintf

--- errorlevel 1


I'm just a beginner. Any help would be appreciated.
Jul 24 2005
next sibling parent Bertel Brander <bertel post4.tele.dk> writes:
xyk wrote:

 #include "myPrint.c"
You don't normally include .c files, but thats not the problem
 void myPrintf();
A prototype for myPrintf, note the f in the end.
 int main()
 {
 int i=1;
 myPrintf();
Call myPrintf, again note the f in the end.
 Here is the function myPrintf(), in a file named "myPrintf.c":
 void myPrint()
Then a myPrint function, but no f in the end...
 {
 printf("ok!");
 }
 
You better add: #include <stdio.h> to myPrint.c, at the top. I C there is a differece between foo() and foo(void), better add void to myPrintf, both the function and the prototype. /b
Jul 24 2005
prev sibling parent mx0 seznam.cz writes:
1) you don't need to declare myPrintf in the "tmpmain.c" - just remove the line 
void myPrintf();
it's useless in tihs case and it's only confusing here (see next, you only have
the function declared but not defined, therefore the compiler message is
correct).

2) there is missing "f" at the end of function name "myPrint()" in the
"myPrintf.c".

3) use
#include <stdio.h>
in the "myPrintf.c"

4) most better approach will be to use header (.h) files instead of including .c
files in another .c's

5) (hint) do not declare functions in pure C like "func()", pure C conformance
requires declaring "func(void)" if the function has none parameters (most C
compilers compiles even "func()", but some don't and the compiler is not liable
to compile such code)
Jul 25 2005