LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2007, 12:08 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c++ "undefined reference"


Hi,
i am new to C++. Attempting to compile my code, i get the following output. Could somebody help me out with this "undefined reference" error?
Code:
kostas@home:~/programming/c++$ g++ ctest3.cc
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/fstream.h:31,
                 from ctest3.cc:2:
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
ctest3.cc:48:2: warning: no newline at end of file
/tmp/cc8HjuEt.o: In function `main':
ctest3.cc:(.text+0x7a): undefined reference to `bit_file_c::bit_file_c()'
ctest3.cc:(.text+0x8d): undefined reference to `bit_file_c::Open(char const*, BF_MODES)'
ctest3.cc:(.text+0x15b): undefined reference to `bit_file_c::GetBit()'
ctest3.cc:(.text+0x189): undefined reference to `bit_file_c::Close()'
ctest3.cc:(.text+0x1e3): undefined reference to `bit_file_c::~bit_file_c()'
ctest3.cc:(.text+0x1f9): undefined reference to `bit_file_c::~bit_file_c()'
collect2: ld returned 1 exit status
The program uses a bit manipulation/editing library, i have found in http://michael.dipperstein.com/bitlibs/#bitfile_cpp
Both the .cpp and .h files have been placed in the same directory, as my source (.cc) file. Heres' my code:
Code:
#include "bitfile.h"
#include <fstream.h>
#include <sys/stat.h>
#include <iostream.h>
#include <string.h>

#define EXIT_FAILURE 1

using std::string;
using namespace std;


int main()
{
	bit_file_c bf;
	int i, value;
	
	try
	{
		bf.Open("/home/kostas/lzw/dict.txt", BF_READ);
	}
	catch (const char *errorMsg)
	{
		cout << errorMsg << endl;
		return (EXIT_FAILURE);
	}
	catch (...)
	{
		cout << "Unknown error opening file" << endl;
		return (EXIT_FAILURE);
	}	


	for (i = 0; i < 100; i++)
	{
		value = bf.GetBit();
		if(value == EOF)
		{
			cerr << "error: reading bit" << endl;
			bf.Close();
			return (EXIT_FAILURE);
		}
		else
		{
			cout << "read bit " << value << endl;
		}
	}
}
 
Old 01-15-2007, 12:45 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Linker

You're getting a linker error. Try either: (1) compiling all the source files together with one call to the compiler, or (2) compile each source file to object files, then compile all the object files together.

(1) g++ -g -Wall ctest3.cc bitfile.cpp
(2) g++ -g -Wall -c ctest3.cc; g++ -g -Wall -c bitfile.cpp; g++ -g *.o

I believe those commands should set you right.
 
Old 01-15-2007, 01:07 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The error "undefined reference to ..." means the linker can't find the object code for some symbol which is used in your program, in this case the implementation of bit_file_c::bit_file_c() and others mentioned in the error message.

I downloded the bitfile code from the page you linked to. Did you build it yet (using the make command)? If so, you should have a file called bitfile.o. This is the object file containing the necessary object code. All you have to do is tell the compiler to look in this file:

Code:
g++ ctest3.cc bitfile.o -o ctest3
Other minor things: The C++ header files should not have the .h suffix. Since the C++ standard was made, youo should be including headers like this (note some old C headers still use the .h suffix) :
Code:
#include "bitfile.h"
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <string>
It generally works with the .h, but the namespaces will probably not be set up properly.

One last thing, I personally prefer to call all C++ files .cpp rather then .cc. This seems to be the norm these days.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"undefined reference"linker error for static field w/ C++ astorm Programming 5 08-27-2008 03:00 AM
Error: Make on kismet-2005-08-R1 "undefined reference" zeevolking Linux - Software 1 12-23-2005 08:40 PM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
Kernel make bzImage error "undefined reference" w/ Prism2.5 USB driver under 2.6 joachimvb Mandriva 1 09-24-2004 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration