Problem background:
gcc v 4.1
2 .cpp files, 2 .h files
Files:
main.cpp
a.cpp
a.h
b.h
Organization:
main.cpp includes a.h (because it calls a.cpp code)
a.cpp includes a.h
a.h includes b.h (because a class in a.h uses a b.h class)
There is no inheritance between a.h or b.h or any of the classes.
The problem is that when I try to compile the files like
g++ main.cpp a.cpp
I get an bunch of linker errors like "duplicate symbol X found..." which are for symbols from b.h
Normally I would think this is due to there not being #ifndef or #pragma once guards, but there are "#pragma once" things at the top of all the .h files. To me it seems like what is happening is that gcc is generating main.o separately from a.o and then then ld is called it sees duplication of symbols and it errors out. What is the correct way to tell g++ to compile them both while looking at them both?
I would think it would be something like --combine, but the man page and testing show that's only valid for C files.
I have also tried -shared which makes ld not complain but which says it's for making a shared library, so it doesn't seem correct for building an executable. Will using -shared lead to any strange behavior down the road (I need to know now since the application itself isn't in a state that I can test it, and I don't want to end up finding out it was the -shared flag's fault)
I have also tried -fwhole-program, which again works, but the man page says it's for optimization, so that also doesn't seem correct. Same question with regards to possible weird behavior down the road.
Thanks much for any help.
John
3 1933 oler1s 671
Recognized Expert Contributor
You probably defined a global variable in b.h. Since main.cpp includes a.h, which includes b.h, it gets one definition of the global. Same for a.cpp. Then, when linking, the two definitions collide. Am I right about the global?
You have to put inclusion guards in your header files.
Sometimes a good way to find out an error is to drop it into another compiler. Different compilers tell you different things. This helped me a few times. - CTYD
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: hvaisane |
last post by:
Valgrind says
==11604== Invalid read of size 4
==11604== at 0x8048ABB: main (foo.cc:36)
==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd
==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156)
==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86)
==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
|
by: atv |
last post by:
Whatis the proper way to handle errors from function calls?
For example, i normally have a main function, with calls to mine
or c functions. Should i check for errors in the functions called
themselves, or should i return a code to main and handle the error
there?
If i don't return them to main, except for the structure, what
use is the main function except for calling functions?
|
by: ktelep |
last post by:
I'm trying to build python 2.4.2 on AIX 5.3 with the IBM xlC compiler
as per the notes in the AIX Readme file. Basically my make fails with
the errors listed below.
I'm calling configure with:
../configure --with-gcc="xlc_r -q64" --with-cxx="xlC_r -q64"
--disable-ipv6 AR="ar -X64"
It looks like it's not resolving symbols in readline. I originally
|
by: Henryk |
last post by:
Hey there,
I have some problems with the following code snippet on a Virtex-4
PowerPC with a GCC based compiler
char chData;
sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???
sprintf(&chData, "%+05.0f", -0.380f); --I get "-000" ???
sprintf(&chData, "%+05.0f", -3.800f); --I get "-0004" ok
|
by: Cao Yi |
last post by:
Hi,
here's a fract of codes, and what's the line "scanf("%lf%*",
&cvi)" doing?
=============================
do {
printf("\nCoefficient: ");
scanf("%lf%*", &cvi);
getchar();
} while (cvi <= 0.0);
| |
by: Jagadish Kumar, Maripi |
last post by:
Hi,
I got the following error while trying to compile php with mysql. I would be
glad if anyone can help me on this. Here is the error that i've got in
config.log
The command i used is:
../configure --prefix=/opt1/php.5.2.1 --with-oci8=instantclient,/opt1/oracle_instantclient_10_2
\
--with-apxs2=/spare/WWW2/bin/apxs --enable-sigchild --with-libxml-dir=/opt1/xml2
|
by: Heinrich Pumpernickel |
last post by:
what does this warning mean ?
#include <stdio.h>
int main()
{
long l = 100;
printf("l is %li\n", l * 10L);
|
by: Neil |
last post by:
What I am doing wrong
This works
batPointer = adaptors.adaptor->batData;
adaptors.batteries = batPointer->battery;
where:
batData is a pointer to a struct
batPointer is a pointer to a different kind of struct
|
by: Rich Healey |
last post by:
AON LAZIO wrote:
Aon,
Please keep these replies on list (I've CC'd the list now).
This isn't actually a very long error message, but since email is a
scrollable medium including long messages often isn't too bad. If you're
worried, include a link to a pastebin.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |