moduler design with "extern keyword"

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

moduler design with "extern keyword"

Post by asif_rahman0 »

Can someone help me? just practicing with a program. It is compiling but not working when i run it. Here i paste it(three parts).
part 1

Code: Select all

////////////////////////
//file name is : m1.h
////////////////////////
#ifndef	__M1_H__
#define	__M1_H__

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

extern char* LCase(char *str);
extern char* UCase(char *str);

#endif
part 2

Code: Select all

//////////////////////////
//file name is : m1.cpp
//////////////////////////
#include "m1.h"

char* UCase(char *str)
{
	int len=strlen(str);
	for(int i=0;i<len;i++){
		str[i]=toupper(str[i]);
	}
	return str;
}
char* LCase(char *str)
{
	int len=strlen(str);
	for(int i=0;i<len;i++){
		str[i]=tolower(str[i]);
	}
	return str;
}
part 3

Code: Select all

/////////////////////////
//file name is : main.h
/////////////////////////

#include <stdio.h>
#include "m1.h"

void main()
{
	printf("This is a test\n");
	printf("%s\n",UCase("This is a test of UCase"));
	printf("%s\n",LCase("This is a test of LCase"));
}
And when i run the program for *.EXE file then it shows the following error. i dont understand it.

Code: Select all

--------------------Configuration: main - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "char * __cdecl LCase(char *)" (?LCase@@YAPADPAD@Z)
main.obj : error LNK2001: unresolved external symbol "char * __cdecl UCase(char *)" (?UCase@@YAPADPAD@Z)
Debug/main.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

main.exe - 3 error(s), 0 warning(s)
what is the reason? plz tell.

bye
rabbi
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

You have to link m1.o with main.o (no idea how to do it in VC, but it should be easy).
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...
Kire Sopov
New poster
Posts: 7
Joined: Wed Sep 15, 2004 2:01 am

Post by Kire Sopov »

Doesn't seem to have a problem in VS. I hope you meant "main.cpp" for the name of the file where the main() function is. I compiled it using VC++ 8.0 with default compiler settings. However, the program crashes during runtime. You should change the main file to something like the following:

Code: Select all

#include <stdio.h> 
#include "m1.h" 

void main() 
{
   char szUpper[] = "This is a test of UCase";
   char szLower[] = "This is a test of LCase";
   printf("This is a test\n"); 
   printf("%s\n",UCase(szUpper)); 
   printf("%s\n",LCase(szLower));

   // Writing to a char * declared like this:
   // char *pChar = "abcdefg";
   // is illegal
   // *pChar = 'x';   // <- runtime error
}
Post Reply

Return to “C++”