473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Order of cpp files matters?

I have a situation where the order of my cpp files in my g++ compile
command line affects the program.

Here is a simplified example:

g++ -Wall -o test a.cpp b.cpp

compiles, links and runs fine.

g++ -Wall -o test b.cpp a.cpp

compiles, and links fine (no errors or warnings) but returns a
segmentation fault when run.

I did not think that the order of the files mattered.
g++ (GCC) 3.3.4 on Debian Linux 2.4.26-1-386
Jul 22 '05 #1
5 2502
"pooh" <po**@hotmail.com> wrote in message
news:3_0Jd.144571$KO5.33634@clgrps13...
I have a situation where the order of my cpp files in my g++ compile
command line affects the program.

Here is a simplified example:

g++ -Wall -o test a.cpp b.cpp

compiles, links and runs fine.

g++ -Wall -o test b.cpp a.cpp

compiles, and links fine (no errors or warnings) but returns a
segmentation fault when run.
We have no way of discerning the problem without seeing
the code. Some possibilites (these are only guesses)
are:

Somewhere your code is producing undefined behavior
(symptoms of which can include 'seems to work'), which
is being manifested differently with different 'build
orders'.

Perhaps you have global objects in different translation
units which depend upon a certain order of creation
(this order is not specified by the language).

But again, nobody can know for sure without seeing the
code.

I did not think that the order of the files mattered.


It can.

-Mike
Jul 23 '05 #2
pooh wrote:
I have a situation where the order of my cpp files in my g++ compile
command line affects the program.

Here is a simplified example:

g++ -Wall -o test a.cpp b.cpp

compiles, links and runs fine.

g++ -Wall -o test b.cpp a.cpp

compiles, and links fine (no errors or warnings) but returns a
segmentation fault when run.

I did not think that the order of the files mattered.
g++ (GCC) 3.3.4 on Debian Linux 2.4.26-1-386


When linking, some linkers only perform a single pass.
This may have something to do with external dependencies,
things referenced outside of a given translation unit.

In some linkers, the object files have to be listed more
than once in order to satisfy undefined symbols.

Try this:
g++ -Wall -c -o a.o a.cpp
g++ -Wall -c -o b.o b.cpp
g++ -o test a.o b.o

also try this:
g++ -o test b.o a.o
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #3
Thomas Matthews wrote:
pooh wrote:
I have a situation where the order of my cpp files in my g++ compile
command line affects the program.

Here is a simplified example:

g++ -Wall -o test a.cpp b.cpp

compiles, links and runs fine.

g++ -Wall -o test b.cpp a.cpp

compiles, and links fine (no errors or warnings) but returns a
segmentation fault when run.

I did not think that the order of the files mattered.
g++ (GCC) 3.3.4 on Debian Linux 2.4.26-1-386

When linking, some linkers only perform a single pass.
This may have something to do with external dependencies,
things referenced outside of a given translation unit.

In some linkers, the object files have to be listed more
than once in order to satisfy undefined symbols.

Try this:
g++ -Wall -c -o a.o a.cpp
g++ -Wall -c -o b.o b.cpp
g++ -o test a.o b.o

also try this:
g++ -o test b.o a.o

I think found the cause of the problem but I'm uncertain as to the reason.

(Sorry but I'm new to c++ so my terminology below may not be quite accurate)

In one of my class headers (.h file) I have an extern statement to allow
the class to be initialized globally.

(in my A.h file):
class A
{ <snip> }

extern A gclA;

I initialize a global object using this class at the end of the
corresponding cpp file.

(in my A.cpp file):
<snip>
A gclA();

But if I switch the g++ command line order of A.cpp with one that is
dependent on it (B.cpp) the program compiles and links but generates a
segmentation fault when run. The opposite order works fine.

Today I found that if I move the initialization of the global object
from A.cpp to main.cpp (which contains the main() function) then the
order of A.cpp and B.cpp no longer causes a problem.

I guess the logic is simply that the program linking somehow determines
when my global object gets initialized and if I get the order wrong the
dependent code fails? Seems odd to me but then again, I think global
variables should be avoided (and this is maybe one of the many reasons
why?).

I'm tempted to pass all "globals" as parameters. Is this practical?

What is a "translation unit". That term is new to me (as is c++).

Thanks
Jul 23 '05 #4

pooh wrote:
I guess the logic is simply that the program linking somehow determines when my global object gets initialized and if I get the order wrong the dependent code fails? Seems odd to me but then again, I think global
variables should be avoided (and this is maybe one of the many reasons why?).

Pooh,

Try not to use global variables in your program. If you absolutely
must access something globally, you can use the singleton pattern:

YourClass& getMySingleton()
{
static YourClass object;
return object;
}

Now you access your object through this function call, e.g.:

getMySingleton().doSomething(...);

This pattern ensures that your object is always fully constructed
before it is used.

I'm tempted to pass all "globals" as parameters. Is this practical?

Not if you use the singleton pattern (see above).
What is a "translation unit". That term is new to me (as is c++).

Thanks


To put it simply, the translation unit is what your compiler sees after
all your header files have been included. The compiler treats the
whole thing as a single "file" and will generate object code (.o file)
for it. Then the object code for all translation units will be linked
to produce your executable file.

-shez-

Jul 23 '05 #5
pooh wrote:
[snip] In one of my class headers (.h file) I have an extern statement to allow
the class to be initialized globally.

(in my A.h file):
class A
{ <snip> }

extern A gclA;

I initialize a global object using this class at the end of the
corresponding cpp file.

(in my A.cpp file):
<snip>
A gclA();

Note: The above does not define the global object, but it
declares a function called gclA, which takes no parameters and
returnes an A object. You ment

A gclA;
But if I switch the g++ command line order of A.cpp with one that is
dependent on it (B.cpp) the program compiles and links but generates a
segmentation fault when run. The opposite order works fine.

Today I found that if I move the initialization of the global object
from A.cpp to main.cpp (which contains the main() function) then the
order of A.cpp and B.cpp no longer causes a problem.

All of this points to one possible conclusion:
There is something else wrong in your program.
It might be that you have an array overflow somewhere in your program
or you access memory with an invalid pointer or things like that.

The reason is:
Linking in different order or moving variables around don't alter
the logical structure of your program but they do alter the specific
memory layout of your program (where in memory are what things
located). You now have found that in specific memory layouts
your program crashes while in others it doesn't. The key to this
is: Usually things are not put in sequence in memory but the compiler
(or linker) inserts some padding bytes between them. In some of
these memory layouts your 'bug' cannot be seen, because you simply
alter some of those padding bytes und thus do no harm to the real
program or variables, while in other configurations
you overwrite vital program information which happens just to be
at those 'hot' areas, because of the specific memory layout.
I guess the logic is simply that the program linking somehow determines
when my global object gets initialized and if I get the order wrong the
dependent code fails?
Could also be:
If you have multiple global objects and their initialization
order is dependent on each other. Avoid such cases if possible.
You can easiliy detect things like that by putting breakpoints
on all constructors of all the global variables and see in
which order they get called.
Seems odd to me but then again, I think global
variables should be avoided (and this is maybe one of the many reasons
why?).

I'm tempted to pass all "globals" as parameters. Is this practical?

What is a "translation unit". That term is new to me (as is c++).


It is one unit that is fed to the compiler independent of all
other units. Usually it is a single cpp source code file.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
15
by: | last post by:
The data file is a simple Unicode file with lines of text. BCP apparently doesn't guarantee this ordering, and neither does the import tool. I want to be able to load the data either sequentially...
0
by: Oci-One Kanubi | last post by:
Everything works fine in Access, but when I double-click on the resultant Excel files the first one opens correctly, but subsequent ones, and any other Excel files I try to open, fail to display at...
3
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
2
by: Al Reid | last post by:
I'm using VB 2005. I have a production application where I load a ListView with information from several sources based on user interaction. At some point I need to iterate through the Items...
3
by: Hartmut Dippon | last post by:
Hi all, I hope somebody can help me with following problem: I have an application where I can drag&drop files/dirs from within explorer onto my form. If multiple files/dirs are selected I...
1
by: aRTx | last post by:
<? /* Directory Listing Script - Version 2 ==================================== Script Author: Artani <artan_p@msn.com>. www.artxcenter.com REQUIREMENTS ============ This script requires...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.