5

For some reason, I should use gcc to compile a C file, then link against Visual C++ 2008 project.

(I used the current latest gcc version: cygwin gcc 4.3.4 20090804.)

But there is one problem: gcc always allocate a big array with _alloca,

and VC linker can't resolve the symbol __alloca.

for example,

int func()
{
    int big[10240];
    ....
}

this code makes the _alloca dependency although I didn't call the _alloca function explicitly.

(array size matters. if i change 10240 -> 128, everything ok)

I tried gcc option -fno-builtin-alloca or -fno-builtin, but no luck.

Is it possible to make gcc not to use _alloca ? (or adjust the threshold?)

Lazer
  • 90,700
  • 113
  • 281
  • 364
shkim
  • 1,173
  • 3
  • 16
  • 24
  • Why not just use `malloc()` explicitly? – Carl Norum Feb 27 '10 at 17:03
  • It's not my written source and somewhat complex to edit. and maybe the original author did not want the penalty to call malloc. – shkim Feb 27 '10 at 17:10
  • 1
    The purpose of alloca in this case is to make sure the stack is large enough to hold this array. The Microsoft compiler doesn't need alloca; it emits this stack probe code directly into the function. The stack probe isn't needed when the locals are small. What happens if you don't use -fno-builtin-alloca and -fno-builtin? – Tim Robinson Feb 27 '10 at 17:43
  • 2
    Oh dear, 10k is a **huge** object to put on the stack, and is a horrible idea regardless of whether it works or not. Can you change the code not to do this? – ephemient Feb 27 '10 at 17:46
  • 1
    @ephe: (1) It's not his code. (2) Changing to `malloc` without appropriate `free` causes leak. – kennytm Feb 27 '10 at 19:04
  • Actually, it's more than 10k: if an `int` is 4B, then this array is 40kB. While it may not cause immediate problems on your current platform, it's still inadvisable. http://stackoverflow.com/questions/1847789 http://stackoverflow.com/questions/2118011 – ephemient Feb 27 '10 at 19:42
  • @Tim: specifying -fno-builtin* or not did not affect anything. I read _alloca is built-in, but I wonder why -fno-builtin doesn't disable it. – shkim Feb 27 '10 at 19:49
  • @ephe: 10240 is an example. "char big[4096]" also generates _alloca call. – shkim Feb 27 '10 at 19:52

4 Answers4

7

Best thing to do would be to compile all code with VC++. If that's not possible..

You should use the mingw gcc instead of the cygwin one. It's designed to output code that will be linked against the VC++ runtime, not the cygwin libraries. In particular, it will call the VC++ runtime function __chkstk instead of __alloca.

andrewffff
  • 579
  • 2
  • 6
  • I tried MinGW (gcc 3.4.5 mingw-vista special r3) and compiled a simple C source. but the dumpbin result showed __alloca dependency: dumpbin /symbols test.obj ... 00C 00000000 UNDEF notype External | __alloca ... is there a special option for mingw? – shkim Feb 27 '10 at 19:41
  • Ah, sorry. The mingw.org gcc does this. The TDM GCC 4.x mingw build outputs __chkstk. http://www.tdragon.net/recentgcc/ – andrewffff Feb 27 '10 at 20:29
  • Thank you. I checked TDM gcc always outputs __chkstk, but VC2008 outputs _chkstk on Release build, and _alloca_probe on Debug build. I think I should edit the code to use local varaibles under 4K. – shkim Feb 27 '10 at 21:33
3

You could just write your own _alloca routine and link against that. Look at the gcc library source to see what it's supposed to do.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • When I implemented _alloca in VS2008, I got: error C2169: '_alloca' : intrinsic function, cannot be defined – shkim Feb 27 '10 at 19:43
  • I think you'd probably want to implement your custom _alloca using gcc rather then Visual Studio. – Paul R Feb 27 '10 at 23:34
1

It looks like _alloca has been deprecated by Microsoft and is no longer in their runtime libraries after VS2005. Newer runtime libraries support _malloca.

Your options don't look good. You can try to build with VS2005 instead. Perhaps cygwin has an option where you can tell it you are using a newer runtime library (and if they don't support that yet, you could file it as a feature request).

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
0

some related discussions:

Lazer
  • 90,700
  • 113
  • 281
  • 364