You are not logged in.

#1 2006-12-25 10:42:26

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

[c++] Include problem causing compiling error [solved]

Hello.
I am working on some code snippets that I finally want to put together.
There are three relevant files that cause the compiling error:

mpdscrob.cpp
options.h
librc.h

Here are the include-Directives of each file:
mpdscrob.cpp

#include <iostream>
#include <unistd.h> /* sleep() */
#include <mysql/mysql.h>
#include "mpd.h"
#include "options.h"

options.h

#include <string>
#include "librc.h"

librc.h

#include <string>
#include <fstream>
#ifdef DEBUG
#include <iostream>
#endif

I build the program via this makefile:
makefile

VERSION = 0.1
CC      = /usr/bin/g++
CFG     = `mysql_config --cflags --libs`

all: options.o mpd.o mpdscrob.cpp
        $(CC) mpdscrob.cpp mpd.o options.o -o mpdscrob $(CFG)

options.o: librc.o options.h options.cpp
        $(CC) options.cpp librc.o -c -o options.o

mpd.o: mpd.h mpd.cpp
        $(CC) mpd.cpp -c -o mpd.o

librc.o: librc.h librc.cpp
        $(CC) librc.cpp -c -o librc.o

clean:
        rm mpd.o
        rm mpdscrob
        rm librc.o
        rm options.o

As you see mpdscrob needs options.o to compile (mpd.o doesn't cause any trouble) and options.o needs librc.o to compile.

I can compile options.o without trouble but when I want to compile all (say mpdscrob), g++ says:

/usr/bin/g++ mpdscrob.cpp mpd.o options.o -o mpdscrob `mysql_config --cflags --libs`
options.o: In function `Options::read_from_file(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
options.cpp:(.text+0x14): undefined reference to `Rc_File::Rc_File()'
[...]
collect2: ld gab 1 als Ende-Status zurück
make: *** [all] Fehler 1

And many other lines that complain about missing references to librc.h which contains the class Rc_File, etc.
Well... I don't know why g++ need to know about these references because they're already included into options.o but, well. I just added this to mpdscrob.cpp:

#include "librc.h"

But then g++ is complaining about redefinition:

/usr/bin/g++ mpdscrob.cpp mpd.o options.o -o mpdscrob `mysql_config --cflags --libs`
librc.h:10: Fehler: Redefinition von »class Rc_File«
librc.h:10: Fehler: vorherige Definition von »class Rc_File«
make: *** [all] Fehler 1

I am quite new to compiling and don't know how to solve this the proper way... well... no I don't know how to solve this at all.

Thankful for every suggestion.
harlekin (:


Hail to the thief!

Offline

#2 2006-12-25 17:57:03

IceRAM
Member
From: Bucharest, Romania
Registered: 2004-03-04
Posts: 772
Website

Re: [c++] Include problem causing compiling error [solved]

First, header files must be written between defines, to prevent double inclusion

#ifndef MY_HEADER
#define MY_HEADER
// ...
#endif

Whenever you use that functions defined in the header, you include it. You will not have to worry about double definitions again though.

It is good to compile each C/C++ file to obtain the object file and, afterwards, to link all the object files together.

When compiling an object file with -c, you
* don't have to tell the compiler which files it has to link (see your rule for options.o). You can do the linking at the end, for the executable
* don't have to specify the output file with -o, the output is automatically named after the source file

The rule making the executable should be named as the executable. "all" should depend on the executable rule.

If I were in your place, I would have my Makefile like this

VERSION = 0.1
CC      = /usr/bin/g++
CFG     = `mysql_config --cflags`
LFG      = `mysql_config --libs`

all: mpdscrob

mpdscrob: options.o mpd.o mpdscrob.o
        $(CC) $(LFG) mpdscrob.o mpd.o options.o -o mpdscrob

mpdscrob.o: mpdscrob.cpp
        $(CC) $(CFG) mpdscrob.cpp -c

options.o: options.h options.cpp
        $(CC) options.cpp -c

mpd.o: mpd.h mpd.cpp
        $(CC) mpd.cpp -c

librc.o: librc.h librc.cpp
        $(CC) librc.cpp -c

clean:
        rm *.o mpdscrob || exit 0

I use "exit 0" because rm returns with exit_code!=0 when it doesn't find something to remove and make fails (I don't like that).

I don't know if this stuff solves your problems. It's the way I do it and it's pretty clean. I rarely have problems with this.

Offline

#3 2006-12-26 08:23:31

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: [c++] Include problem causing compiling error [solved]

The problem is in *linkage*, not compilation. I.e. the needed libraries are not listed in g++ command.

Offline

#4 2006-12-26 13:14:50

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: [c++] Include problem causing compiling error [solved]

And how do I tell where to find them?
Tried to add the header files to the g++ arguments:

mpdscrob: mpdscrob.o mpd.o options.o
    $(CC) $(LFG) -o mpdscrob librc.h mpdscrob.o mpd.o options.o

But the error persists.
I overflew `man g++` but couldn't find an answer either.
Seems to be a general problem of understanding the process. :

The missing references it is complaining about are documented in librc.h which is in the same directory as all other files.

IceRAM: Thanks, I improved my makefile and some header files concerning your points. (:


Hail to the thief!

Offline

#5 2006-12-26 19:11:54

codemac
Member
From: Cliche Tech Place
Registered: 2005-05-13
Posts: 794
Website

Re: [c++] Include problem causing compiling error [solved]

If you did what IceRAM said you should be getting different errors now.

Offline

#6 2006-12-27 10:59:29

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: [c++] Include problem causing compiling error [solved]

<code>#include "librc.h"</code> - what library does it belong to? Look manual for the library, generally, you'll need to add -lrc or something like this to the g++ command line

Offline

#7 2006-12-27 11:13:05

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: [c++] Include problem causing compiling error [solved]

codemac: No, the error is still the same. Why it should be anything differend?

drakosha: librc is written by myself. It is comprised of these files:

librc.h
librc.cpp

The file options.cpp needs this librc and as for that I included it in options.h.
If I finally want to link options.o and mpdscrob.o g++ (still) is complaining about missing references to librc:

/usr/bin/g++ `mysql_config --libs` -o mpdscrob librc.h mpdscrob.o mpd.o options.o
[...]
options.cpp:(.text+0x14): undefined reference to `Rc_File::Rc_File()'
options.cpp:(.text+0x29): undefined reference to `Rc_File::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
options.cpp:(.text+0x195): undefined reference to `Rc_File::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[...]

I just did a change in the makefile and now it's working:

all: mpdscrob

mpdscrob: librc.o options.o mpd.o mpdscrob.o
        $(CC) $(LFG) -o mpdscrob librc.o mpdscrob.o mpd.o options.o

I was not away of that I had to include librc.o as well because I thought it is already included into options.o because I compiled it like this:

options.o: options.h options.cpp
        $(CC) -c -o options.o options.cpp

Well... I could have thought of it but thanks anyway. I guess my make file and my header files are a little smarter now and my programming style needs a lot of smartness. :

Thanks! (=


Hail to the thief!

Offline

Board footer

Powered by FluxBB