473,809 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the proper way to combine C++ files (with g++) to avoid link (ld) errors?

1 New Member
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
Feb 29 '08 #1
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?
Feb 29 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You have to put inclusion guards in your header files.
Mar 1 '08 #3
CodeTilYaDrop
66 New Member
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
Mar 2 '08 #4

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

Similar topics

2
22834
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*,...
4
2476
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?
0
1762
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
12
4610
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
9
2694
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);
5
3071
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
92
6268
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);
7
2636
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
0
2025
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.
0
9721
marktang
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...
0
9601
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,...
0
10635
Oralloy
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...
1
10378
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,...
0
9198
agi2029
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...
1
7653
isladogs
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...
0
5550
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...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.