I have a 10+ years old C library which -- I believe -- used to work just fine in the good old days, but when I tried to use it with a C++ source (containing the main function) the other day I ran into some difficulties.
Edit: to clarify, the C library compiles just fine with gcc
, and it generates an object file old_c_library.o
. This library was supposed to be used in a way so that the C header file old_c_library.h
is #include
d in your main.c
C source file. Then your main C source file should be compiled and linked together with old_c_library.o
via gcc
. Here I want to use a C++ source file main.cpp
instead, and compile/link it with g++
.
The following three problems occurred, during the compilation of the C++ source file:
- one of the header files of the C library contains the C++ reserved word
new
(it is the name of an integer), which resulted in fatal error; and - one of the header files of the C library contains a
calloc
call (an explicit typecast is missing), which resulted in fatal error; and - various files of the C library contain code where comparison of signed and unsigned integers happen, which resulted in warnings.
Edit: I tried to use the #extern "C" { #include "obsolete_c_library.h" }
"trick", as suggested in the comments, but that did not solve any of my problems.
I can sort out problem 1 by renaming all instances of the reserved words and replacing them by -- basically -- anything else. I can sort out problem 2 by typecasting the calloc
call. I might try to sort out the warnings by ideas suggested here: How to disable GCC warnings for a few lines of code.
But I still wonder, is there a way to overcome these difficulties in an elegant, high-level way, without actually touching the original library?
Relevant: Where is C not a subset of C++? and Do I cast the result of malloc? and How do I use extern to share variables between source files?.