I created a static library on Windows using Visual Studio 2008 with these two files:
header file was a simple C API:
#ifdef __cplusplus
extern "C" {
#endif
void init();
void stop();
#ifdef __cplusplus
}
#endif
Implementation however was using C++ features:
#include "my_lib_api.h"
#include <iostream>
void init() {
std::cout << "Initializing my_lib\n";
}
void stop() {
std::cout << "stopping my_lib\n";
}
I then built with these commands:
mkdir Release
cl.exe /O2 /Oi /GL /D "WIN32" /D "NDEBUG" /D "_LIB" /FD /EHsc /MT /Gy /Fo"Release\\" /W3 /c /Zi /TP my_lib_api.cpp /nologo /errorReport:prompt
lib.exe /OUT:my_lib_api.lib /LTCG .\Release\my_lib_api.obj /NOLOGO
I then copied my_lib_api.lib and my_lib_api.h to a new folder where I created a new Visual Studio console application with a main.c source file:
#include "my_lib_api.h"
int main(int argc, char* argv[]) {
init();
stop();
return 0;
}
I built using same /MT multi-threaded C runtime library to be consistent with library. It then built like this:
mkdir Release
cl.exe /O2 /Oi /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /FD /EHsc /MT /Gy /Fo"Release\\" /W3 /c /Zi /TC .\main.c /nologo /errorReport:prompt
successfully created Release/main.obj
link.exe /OUT:c_api_so.exe /INCREMENTAL:NO /MANIFEST:NO /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE /NXCOMPAT /MACHINE:X86 my_lib_api.lib kernel32.lib user32.lib shell32.lib .\Release\main.obj /NOLOGO /ERRORREPORT:PROMPT
Successfully created c_api_so.exe which runs just fine.
This means I can write a library in C++ and as long as I provide a C API then both C and C++ calling programs can use my library? Is this universally true? Should this always work? Why does it work? I assume that for each platform you would say only support one compiler/linker and provide eg on Windows a Visual Studio based library and on linux a GNU C based library? Are there any issues to be aware of?