-
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$";
:confused:
-
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
-
> got a linking problem I guess.
My guess is we've no idea which linker you're using.
-
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.