linker mayhem

This is a discussion on linker mayhem within the C++ Programming forums, part of the General Programming Boards category; got a linking problem I guess. I have a string defined in an object file that I want the linker ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    linker mayhem

    got a linking problem I guess. I have a string defined in an object file that I want the linker to put in the final executable, but nothing refers to the symbol and the linker throws it away

    Code:
    static const char version_str[] = "$Id: 1.2.3$";
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,251
    The obvious fix is to place a reference to that string in some function and (depending on how smart your linker is) ensure that function is called.

    For example;
    Code:
    static const char version_str[] = "$Id: 1.2.3$";
    
    const char *VersionString()
    {
        return version_str;
    }
    
    int main()
    {
        (void)VersionString();
        // whatever else you want
    }
    Very few linkers I'm aware of would be aggressive enough to remove the string from the executable in this case --- particularly if you force the issue by placing the function into a separate source file from the caller. It is obviously technically possible, but unlikely in practice. If the odds work against you, the following will certainly bludgen the compiler and linker into submission.

    Code:
    #include <cstring>
    #include <iostream>
    static const char version_str[] = "$Id: 1.2.3$";
    
    const char *VersionString()
    {
        return version_str;
    }
    
    int main(int argc, char **argv)
    {
        if (argc >= 2 && std::strcmp(argv[1], "-print-version") == 0)
           std::cerr << "Version is " << VersionString() << '\n';
        // whatever else you want
    }
    It would be a very brave compiler and linker that would remove the reference to your version string from the executable in this case, as the string may need to be displayed in result to a user action.

    If your executable still does not have the string in the required form, you will need to dig into your system documentation. The only case I can think where this would occur is if you use an executable compressor

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > got a linking problem I guess.
    My guess is we've no idea which linker you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Location
    Atlanta, GA.
    Posts
    1
    Woops... it's actually cross-platform. Our linkers range from GNU ld on various platforms (x86, ia64) to Sun's linker... hence the omission. I tried using GNU's __attribute__((used)) but apparently that's only for functions.

    I wound up doing something similar to what Grumpy suggested and declaring a static char array in each translation unit:

    static char buildid[] = "$Id: blah blah blah $";

    Seems to work with GNU on i386.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  2. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 12:25 AM
  3. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  4. Linker error using system(*.*)
    By Winston4u in forum C Programming
    Replies: 5
    Last Post: 05-09-2003, 05:54 PM
  5. Compile with *.lib
    By Kelvin in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2002, 06:24 PM

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