The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Avoid creating temporary files on editing a file in Ubuntu royalibrahim Linux 7 11-17-2007 02:57 AM
avoid displaying errors while executing a script vikas027 Shell Programming and Scripting 4 10-29-2007 11:41 AM
combine two files fredao1 Shell Programming and Scripting 1 01-08-2007 06:29 PM
Adapter Errors and Link Errors mcastill66 AIX 2 08-02-2005 03:51 PM
Adapter Errors and Link Errors mcastill66 UNIX for Advanced & Expert Users 0 08-02-2005 03:11 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-29-2008
Registered User
 

Join Date: Feb 2008
Posts: 1
What is the proper way to combine C++ files (with g++) to avoid link (ld) errors?

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
Reply With Quote
Forum Sponsor
  #2  
Old 03-17-2008
Registered User
 

Join Date: Feb 2005
Posts: 53
Maybe you have duplicate global variables defined in header file (e.g. "a.h"). Make sure the all global are declared with "extern" keyword.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0