Compiling a program containing extern "C"

I am trying to use a makefile to compile a program that someone wrote using cygwin. I get a lot of error messages, of which many complain error: template with C linkage .

After searching a bit, it seems the problem is with extern "C" . This line is contained in the cygwin / usr / include / pthread.h file, which is included in #include < pthread.h > in one of the headers. And when I delete this line, most error messages disappear. But there are several leftists, of the following form:

 /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 like to sit down and study all this in detail, but I do not have time before I need this program.

+7
c ++ c header-files extern
source share
3 answers

EDIT: Based on the exchange of comments, the culprit was a header file in the assembly directory (Endian.h), which contradicted the system, including the /usr/include/endian.h file. It is included instead of the system header and causes build problems. Files were in conflict because the case is small in Windows. The main reason was what was suggested in the original answer. The extern C construct was inadvertently leaking into C ++ code, where patterns were defined, causing the specified error.

I would look at the C C binder design in your header files. That would be in the code you wrote (none of the system headers, they are probably safe).

The code in the headers is wrapped,

on top:

 #ifdef __cplusplus extern "C" { #endif 

and below:

 #ifdef __cplusplus } #endif 

If the bottom half is missing, the effect of the top half inadvertently extends to the code in other headers. This causes the problems you are facing.

+8
source share

This problem occurs when your compiler compiles C and C ++ code. The extern "C" syntax is a way to tell the C ++ compiler that the C compiler should also access this function. The C compiler does not understand this use of extern, so usually you hide it like this:

 #ifdef __cplusplus extern "C" { #endif 
void whatever();
#ifdef __cplusplus } #endif

However, you should not change the system headers, the likelihood that they are wrong is very slim. Most likely, one of your own headers does not have a closing brace above.

+7
source share

Similarity to the accepted answer, it was a nesting problem for me, but not with ifdef/endif , so I am adding as a reference for others.

In my case, this error was caused by nested extern "C" constructs; one header file containing the extern "C" construct was included inside another extern "C" construct, resulting in a nesting confusion between the compiler / preprocessor.

Ah file:

 #ifdef __cplusplus extern "C"{ #endif #include "bh" #ifdef __cplusplus } #endif 

Bh file

 #ifdef __cplusplus extern "C"{ #endif void someFunctionDeclaration(); #ifdef __cplusplus } #endif 

Moving #include "bh" outside the ah extern "C" structure fixes the problem.

+1
source share

All Articles