jorgen jorgen - 15 days ago 5
C Question

Compiling program containing extern "C"

I'm trying to use a makefile to compile a program someone else has written, using cygwin. I get a lot of error messages, of which many complain

error: template with C linkage
.

After searching around for a bit it seems the problem is connected to
extern "C"
. This line is contained in the file cygwin/usr/include/pthread.h, which is included with
#include < pthread.h >
in one of the headers. And when I remove this line most of the error messages disappear. But there are a few left, of the following sort:

/usr/include/pthread.h:67:5: error: previous declaration of ‘int pthread_atfork(void (* )(),void ( *)(), void ( *)())’ with ‘C++’ linkage

/usr/include/sys/unistd.h:136:5: error: conflicts with new declaration with ‘C’ linkage


Does anyone know how to fix this? I would love to sit down and learn all this stuff in detail but I don't have time before I need this program running..

Answer

EDIT: Based on the exchange in the comments, the culprit was a header file in the build directory (Endian.h) that conflicted with a system include file /usr/include/endian.h. It was being included instead of the system header, and causing build issues. The files were in conflict because case is insignificant on Windows. The root cause was what was suggested in the original answer. The extern C construct was leaking into C++ code unintentionally, where templates were defined, causing the indicated error.

I would check for a "dangling" C linkage construct in your header files somewhere. This would be in code you wrote (not any of the system headers; those are likely safe).

Code in headers is wrapped with,

on top:

#ifdef __cplusplus
extern "C" {
#endif

and at bottom:

#ifdef __cplusplus
}
#endif

If the bottom part is missing, the effect of the top half extends into code in other headers unintentionally. This causes issues like you are encountering.

Comments