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$";
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
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;
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:static const char version_str[] = "$Id: 1.2.3$"; const char *VersionString() { return version_str; } int main() { (void)VersionString(); // 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.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 }
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
> 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.
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.