g++ -r Confusion

This is a discussion on g++ -r Confusion within the Linux Programming forums, part of the Platform Specific Boards category; I want to take all of the C++ object files in a directory and ball them up into one object ...

  1. #1
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31

    g++ -r Confusion

    I want to take all of the C++ object files in a directory and ball them up into one object file.
    I tried "g++ -r -o out.o in_1.o in_2.o ...", but I get the error message "/usr/bin/ld: cannot find -lgcc_s". What's up with that?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Do you want ar?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by skewray View Post
    I want to take all of the C++ object files in a directory and ball them up into one object file.
    I tried "g++ -r -o out.o in_1.o in_2.o ...", but I get the error message "/usr/bin/ld: cannot find -lgcc_s". What's up with that?
    Use ld directly:

    Code:
    ld -r -o out.o in_1.o in_2.o ...
    EDIT: But yeah, are you sure you don't just want a library? I have only rarely needed to merge object files.

  4. #4
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31
    Ah, that works much better. Thanks! I used to use ar, but I decided I wanted to simplify my build procedure by having each subdirectory make one object file, and then combine those at the top level. Before, I had a directory full of libraries that had to be listed in exactly the right order to the loader.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by skewray View Post
    Ah, that works much better. Thanks! I used to use ar, but I decided I wanted to simplify my build procedure by having each subdirectory make one object file, and then combine those at the top level. Before, I had a directory full of libraries that had to be listed in exactly the right order to the loader.
    The problem with using merged objects instead of libraries is that during library linking, ONLY the .o files (within the libraries) which contain functions that are ACTUALLY REFERENCED are linked. If you merge a bunch of .o files into a monolithic object, this ENTIRE object must either be linked, or not linked.

    So if you want to use only one or two functions from such a "library," the linker is going to pull in EVERYTHING, including a bunch of dead code.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not to mention that you might get name conflicts with internal-only functions. Or does the linker rename them on merging?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by CornedBee View Post
    Or does the linker rename them on merging?
    A bug in that could be interesting... lol.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, to a masochist. To us normal people it would just be a nightmare.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21