I'm in linker paradise now. I have a C library which only compiles in Visual C++ (it probably works in gcc) if:
- I compile it as C++ code
- Define
__cplusplus
which results in all the declarations being enclosed inextern "C" { }
So, by doing this I have a static library called, say, bsbs.lib
Now, I have a C++ project called Tester
which would like to call function barbar
in declared in bsbs.h
. All goes fine, until I try to link to bsbs.lib
where I get the all-too-familiar:
Tester.obj : error LNK2001: unresolved external symbol _foofoo
And it always seems to be foofoo
which cannot be resolved regardless of which function I call in Tester
(barbar
or anything else).
Update: I've expanded on Point 2 as requested. Thanks a lot for the help guys!
#ifndef _BSBS_H
#define _BSBS_H
/* Prevent C++ programs from name mangling these definitions. */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <setjmp.h>
.......
.......
#ifdef __cplusplus
}
#endif
#endif /* _BSBS_H */
This is the "main" header file, so to speak. All the important functions are here. But there are other header files called by the bsbs.c
file which are not enclosed in extern "C" {}
.
Solved:
OK, this is quite weird, but I removed the extern C
bit from the header file in bsbs
, compiled it as a C++ project (even though all the files are .c
and removed the __cplusplus
define) and it worked! I got the idea after looking at the symbol list. Everything was mangled except the ones enclosed in extern C
(doh) and it was asking for an unmangled symbol so I figured something was amiss.