Thread: Help with Borland & Linking

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    13

    Help with Borland & Linking

    Hello...this is my first time posting here, so if I screw up the code tags or something....

    Okay, I'm having trouble getting multiple files to compile with Borland C++Builder 4.0 I followed my school textbook example, but it doesn't give compiler specific instructions. I've tried adding all the files to a project, but I get this error:

    [Linker Error] Unresolved external 'GetSum(int, int)' referenced from C:\PROGRAM FILES\BORLAND\CBUILDER4\PROJECTS\FUNCTION\FUNCTION .OBJ

    Here is the code:

    Code:
    //main.cpp
    
    #include <iostream.h>
    #include <conio.h>
    #include "test.h"
    
    int main()
    {
            int num1=5;
            int num2=5;
            int sum;
    
            sum=GetSum(num1,num2);
            
            cout << sum;
    
            getch();
            return 0;
    }
    Code:
    //getsum.cpp
    #include "test.h"
    
    int GetSum(int num1, int num2)
    	{
    	int sum;
    	sum=num1+num2;
    	return sum;
    	}
    Code:
    //test.h
    #ifndef SUM
    
    int GetSum(int num1, int num2);
    
    #define SUM
    #endif
    Is there a problem with my code, or am I not doing something to compile or link it right? If anyone has experience with C++Builder 4, I could really use your help with this. Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The code seems fine, so the error lies in the linking. You said you added the files to a project, so I see no reason why it shouldn't work though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    xmdvp
    Guest
    you simply forgot to define SUM in your main.cpp:
    Code:
       #define SUM
    because a compile time, because the macro SUM isn't define, the function GetSum isn't compiled!!

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by xmdvp
    you simply forgot to define SUM in your main.cpp:
    Code:
       #define SUM
    because a compile time, because the macro SUM isn't define, the function GetSum isn't compiled!!
    Nope! Notice the n in #ifndef meaning "if not defined". The function declaration is included if SUM is not defined.

    Also, the error he gets is "unresolved external" which appears if you have the declaration but not the definition. If you miss the declaration, you get an "undeclared identifier" message or something.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    Does anyone else have some suggestions that I could try to get this program working? It's very hard trying to continue learning C++ when you can't have multiple file programs...I can't compile half of the examples, and my teacher never taught us anything about using C++Builder4 except making a new project and hitting the compile button.

    At this point, I'm open to any ideas or suggestions. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 01:35 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. Linking error with DirectDrawCreateEx
    By jjj93421 in forum Game Programming
    Replies: 6
    Last Post: 04-06-2004, 03:57 PM