Home > Software engineering >  Getting around undefined symbol: _strnicmp
Getting around undefined symbol: _strnicmp

Time:08-17

I have a library which is a python wrapper for a C/C library, which needs to be imported to Python in Linux. Let's call the library abc.so. This library is dependent on another C/C library: xyz. Both these libraries used to have/have facilities that are dependent on Windows, Borland compiler or similar compilers. I am able to successfully build abc.so, after fixing some of the windows compiler related issues. However, I cannot import it to my python code. I receive the error:

ImportError: /usr/local/lib/abc.so: undefined symbol: _strnicmp

or a variant of this. I tried various import methods involving packages like ctpes, os, sys and flags like RTLD_LAZY, RTLD_GLOBAL, RTLD_NOW under the assumption that the method of import will fix this problem. However, none of them worked. This answer: undefined reference to stricmp (and the comment above) suggests that strnicmp should be replaced. It also points out that this is a link time error. However, I have not been able to identify part of these libraries expecting an implementation of strnicmp. What would be a good approach to find the source of this issue? Also, should I be trying some alternative path to fix this issue?

CodePudding user response:

The stricmp() and strnicmp() functions are specific to Windows.

POSIX (Linux) uses the <strings.h> header and strcasecmp() and strncasecmp().

You can write a simple cover function or change the calls via a macro. For the cover function, you'd want to use appropriate conditional compilation (#ifdef SOMETHING / #endif — or maybe #ifndef SOMETHING / #endif). You might use an inline function if it only appears in one file. If it appears in many files, you may want a regular function, even though it's a smidgeon less efficient.

static inline int strnicmp(const char *s1, const char *s2)
{
    return strcasecmp(s1, s2);
}

or

#undef strnicmp
#define strnicmp(s1, s2) strcasecmp(s1, s2)
  • Related