Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU - Undefined Reference to `setmode'

Tags:

c

linux

gnu

porting

I am porting a WIN32 library on Linux. I am able to compile my library with no error. But when i try to link, it gives following linking error

undefined reference to `setmode'

I know that setmode function is standard library function and it resides in unistd.h and i provide the argument -lc while linking through terminal.

This is the link which lightened more about setmode.

Is there any help for the same?

like image 311
jparthj Avatar asked Oct 22 '25 02:10

jparthj


2 Answers

I'm afraid these answers are nonsense. A program coming from the Win32 environment is using one of two possible setmode functions: either the Microsoft _setmode function documented in MSDN, or the Cygwin imitation of it called setmode.

This has nothing to do with the BSD setmode. It's a two-argument function which sets a file descriptor to either binary or text translation mode.

If your program expects this function, but you link to LibBSD, the program might link, but it is not correct.

The GNU C library doesn't provide an interface for manipulating the text or binary mode of FILE * streams or descriptors, and if it did, it wouldn't do anything, since the two modes are identical.

Code that needs the Cygwin setmode function on Cygwin can probably just get away with not doing anything on Unix: so that is to say:

#ifdef __CYGWIN__
setmode(fileno(my_stdio_stream), O_BINARY);
#elif _WIN32
_setmode(_fileno(my_stdio_stream), _O_BINARY);
#else
/* nothing on systems with no text-vs-binary mode */
#endif
like image 111
Kaz Avatar answered Oct 23 '25 17:10

Kaz


I had trouble with this one too; the linking happens during the make process, so this will occur and people who have no business dealing with c (like me) try to use cygwin or mingw. Thanks to Cairnarvon's answer, I learned that a necessary include statement was missing:

#include <io.h>

...in modules which call the setmode function (in my case, I am trying to build GDAL on Cygwin 64 bit). The error remained, until I saw these folks' answer and learned that the 64 bit version of Cygwin does not prepend underscores the way that the 32 bit version does. So I did the prepending manually and the make finished [more] successfully. I hope this helps.

like image 42
foszter Avatar answered Oct 23 '25 16:10

foszter