By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,371 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,371 IT Pros & Developers. It's quick & easy.

My first [Linker error]

P: 1
Hi,

My first post.

How do you resolve linker errors? I have a simple header file that contains one function declaration. This function is used in another file (.cpp) in the definition of a macro.

There is another separate .cpp file that actually defines the function. I assumed that if I had these three separate source files saved in the same directory (C:\) that my compilier would find the header file along with the other .cpp file and link them together. I did also save the header file in the include directory, which looks like is used by my IDE.

The IDE is Dev-C++ by Bloodshed.net. Seems good.

Anyway the code is as follows:

HEADER, fucntion declaration
Expand|Select|Wrap|Line Numbers
  1. //: C03:printBinary.h
  2. // Display a byte in binary
  3. void printBinary(const unsigned char val);
  4. ///:~ 
  5.  
FUNCTION DEFINITION
Expand|Select|Wrap|Line Numbers
  1.  
  2. //: C03:printBinary.cpp {O}
  3. #include <iostream>
  4. void printBinary(const unsigned char val) {
  5.   for(int i = 7; i >= 0; i--)
  6.     if(val & (1 << i))
  7.       std::cout << "1";
  8.     else
  9.       std::cout << "0";
  10. } ///:~
  11.  
  12.  
FILE with LINKER ERRORS
Expand|Select|Wrap|Line Numbers
  1. //: C03:Bitwise.cpp
  2. //{L} printBinary
  3. // Demonstration of bit manipulation
  4. #include "printBinary.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. // A macro to save typing:
  9. #define PR(STR, EXPR) \
  10.   cout << STR; printBinary(EXPR); cout << endl;  
  11.  
  12. int main() {
  13.   unsigned int getval;
  14.   unsigned char a, b;
  15.   cout << "Enter a number between 0 and 255: ";
  16.   cin >> getval; a = getval;
  17.   PR("a in binary: ", a);
  18.   cout << "Enter a number between 0 and 255: ";
  19.   cin >> getval; b = getval;
  20.   PR("b in binary: ", b);
  21.   PR("a | b = ", a | b);
  22.   PR("a & b = ", a & b);
  23.   PR("a ^ b = ", a ^ b);
  24.   PR("~a = ", ~a);
  25.   PR("~b = ", ~b);
  26.   // An interesting bit pattern:
  27.   unsigned char c = 0x5A; 
  28.   PR("c in binary: ", c);
  29.   a |= c;
  30.   PR("a |= c; a = ", a);
  31.   b &= c;
  32.   PR("b &= c; b = ", b);
  33.   b ^= a;
  34.   PR("b ^= a; b = ", b);
  35. } ///:~
  36.  

The actual error messages, one appears every time the PR macro is used: " [Linker error] undefined reference to `printBinary(unsigned char)' "

the other : " ld returned 1 exit status ".

I have the files saved as "printBinary.h", "printBinary.cpp", "Bitwise.cpp".


I'm using Windows XP. This is my first attempt linking.
Nov 11 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
You have to compile both cpp files and link them together, from you description I assume you are only compiling and linking 1 of them.

The linker is likely going to find standard libraries without being specifically told to do so, it will not automatically find compile and link any extra cpp files you may have defined.
Nov 11 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.