Main Content

Fix Polyspace Linking Errors Related to extern "C" Blocks

Extern C Functions

Some functions may be declared inside an extern "C" { } block in some files, but not in others. In this case, the linkage is different which causes a link error, because it is forbidden by the ANSI® standard.

Original code:

extern "C" {
    void* memcpy(void*, void*, int);
}
class Copy
{
public:
    Copy() {};
    static void* make(char*, char*, int);
};
void* Copy::make(char* dest, char* src, int size)
{
    return memcpy(dest, src, size);
}

Error message:

Pre-linking C++ sources ... 

<results_dir>/test.cpp, line 2: error: declaration of function "memcpy" 
is incompatible with a declaration in another translation unit 
(parameters do not match) 
|            the other declaration is at line 4096 of "__polyspace__stdstubs.c" 
|    void* memcpy(void*, void*, int); 
|          ^ 
|          detected during compilation of secondary translation unit "test.cpp" 

The function memcpy is declared as an external "C" function and as a C++ function. It causes a link problem. Indeed, function management behavior differs whether it relates to a C or a C++ function.

When such error happens, the solution is to homogenize declarations, i.e. add extern "C" { } around previous listed C functions.

Another solution consists in using the permissive option -no-extern-C. It removes all extern "C" declarations.

Functional Limitations on Some Stubbed Standard ANSI Functions

  • signal.h is stubbed with functional limitations: signal and raise functions do not follow the associated functional model. Even if the function raise is called, the stored function pointer associated to the signal number is not called.

  • No jump is performed even if the setjmp and longjmp functions are called.

  • errno.h is partially stubbed. Some math functions do not set errno, but instead, generate a red error when a range or domain error occurs with ASRT checks.

You can also use the compile option POLYSPACE_STRICT_ANSI_STANDARD_STUBS (-D flag). This option only deactivates extensions to ANSI C standard libC, including the functions bzero, bcopy, bcmp, chdir, chown, close, fchown, fork, fsync, getlogin, getuid, geteuid, getgid, lchown, link, pipe, read, pread, resolvepath, setuid, setegid, seteuid, setgid, sleep, sync, symlink, ttyname, unlink, vfork, write, pwrite, open, creat, sigsetjmp, __sigsetjmp, and siglongjmpare.