3

I want to import some C code but override its main() function. I can do this in Unix by prefacing the C code's main declaration with __attribute__((weak)), however, this won't compile in Windows, because neither Strawberry Perl's GCC nor MinGW's GCC recognize __attribute__((weak)).

Reading the docs online, __declspec seems to function similarly. Is there a __declspec equivalent to Unix GCC's __attribute__((weak)) macro?

This is a more specific version of an earlier question I posted.

Community
  • 1
  • 1
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • Why not change the EXE entry point name instead? – Pavel Radzivilovsky Sep 12 '12 at 21:23
  • In MSVC linker options, you can change the entry point name to something else, and name your REAL main function differently. – Pavel Radzivilovsky Sep 12 '12 at 21:26
  • Can you help me to do this either in a Makefile, or the code itself? I don't want to force the code to have to use MSVC++. Hopefully we can get this to work in Cygwin. – mcandre Sep 12 '12 at 21:28
  • In link.exe, it is /ENTRY. http://msdn.microsoft.com/en-us/library/f9t8842e(v=vs.80).aspx I am sure makefile can get it thru, though I am not an expert on makefiles. – Pavel Radzivilovsky Sep 12 '12 at 21:39
  • Would I supply `/entry` when compiling scriptedmain.exe, when compiling test.exe, or both? – mcandre Sep 12 '12 at 21:41
  • Just saying GCC isn't only usable in UNIX. Works great for me all the time in Windows. – chris Sep 12 '12 at 21:43
  • That's why I'm asking this: When I use `__attribute__((weak))` in GCC in Unix (Mac OS X as it happens), everything works just fine. When I try to compile the exact same C code with GCC (MinGW and Strawberry Perl) in Windows, compilation fails. See http://stackoverflow.com/questions/12395130/trouble-using-scriptedmain-in-mingw/12395171 – mcandre Sep 12 '12 at 21:47
  • I have no idea what are these EXEs you mentioned.. – Pavel Radzivilovsky Sep 12 '12 at 21:50
  • Pavel, let's start over. I want to replicate this Perl code behavior, but in GCC/Windows-valid C: https://github.com/mcandre/scriptedmain/tree/master/perl If you run the scriptedmain file, it prints "Main: The meaning of life is 42". If you run the test file, it prints "Test: The meaning of life is 42". For Perl, these files would be scriptedmain.pm and test.pl. For C, these files would be scriptedmain.c, compiled into scriptedmain.exe; and test.c, compiled into test.exe. This works when compiled by a true Unix GCC compiler (eg MacOSX gcc using macro). I want C code that works in Windows. – mcandre Sep 12 '12 at 22:02
  • I found a way to hide `main()` using [simple preprocesser instructions][1]. [1]: http://stackoverflow.com/a/12397886/350106 – mcandre Sep 13 '12 at 00:37

2 Answers2

1

There's another way with MSVC that I think would work if you care to use it.

/*
 * pWeakValue MUST be an extern const variable, which will be aliased to
 * pDefaultWeakValue if no real user definition is present, thanks to the
 * alternatename directive.
 */

extern const char * pWeakValue;
extern const char * pDefaultWeakValue = NULL;

#pragma comment(linker, "/alternatename:_pWeakValue=_pDefaultWeakValue")

See this old SO answer for some other options.

Community
  • 1
  • 1
JimR
  • 15,513
  • 2
  • 20
  • 26
0

There's also __declspec(selectany)

robUx4
  • 853
  • 9
  • 13