about extern "C"

F

flyaflya

I want use a c lib in vc6. so I use extern "C" to c function, include
three files "test.cpp", "b.h" and "b.c":

//************************************************
// test.cpp
#include "stdafx.h"
#include "b.h"
int main(int argc, char* argv[])
{

return b();
}

//************************************************
//b.h
#if !defined __B_HHHHHH
#define __B_HHHHHH

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

extern "C" int b(); //error occur

#endif

//************************************************
//b.c
#include "b.h"
int b()
{
return 111;
}

error:
d:\test\testco\b.h(10) : error C2059: syntax error : 'string'

I think it's simple, but what wrong? owing to my ide?
 
R

Rolf Magnus

flyaflya said:
I want use a c lib in vc6. so I use extern "C" to c function, include
three files "test.cpp", "b.h" and "b.c":

//************************************************
// test.cpp
#include "stdafx.h"
#include "b.h"
int main(int argc, char* argv[])
{

return b();
}

//************************************************
//b.h
#if !defined __B_HHHHHH

This is not a valid identifier. You must not define identifiers that contain
two consecutive underscores, because those are reserved for the compiler
and the standard library.
#define __B_HHHHHH

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

extern "C" int b(); //error occur

#endif

//************************************************
//b.c
#include "b.h"
int b()
{
return 111;
}

error:
d:\test\testco\b.h(10) : error C2059: syntax error : 'string'

I think it's simple, but what wrong? owing to my ide?

extern "C" is not valid in C code, only in C++ code. Try something like:

#ifdef __cplusplus
extern "C"
#endif
int b();
 
F

flyaflya

flyaflya said:
I want use a c lib in vc6. so I use extern "C" to c function, include
three files "test.cpp", "b.h" and "b.c":

//************************************************
// test.cpp
#include "stdafx.h"
#include "b.h"
int main(int argc, char* argv[])
{

return b();
}

//************************************************
//b.h
#if !defined __B_HHHHHH
#define __B_HHHHHH

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

extern "C" int b(); //error occur

#endif

//************************************************
//b.c
#include "b.h"
int b()
{
return 111;
}

error:
d:\test\testco\b.h(10) : error C2059: syntax error : 'string'

I think it's simple, but what wrong? owing to my ide?

i have finded the answer, the file "b.c" can't include "b.h", because
extern "C" just can be compiled in cpp file, can't be parsered in c file.
language C is not same as language CPP usually include head file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,506
Members
48,193
Latest member
DannyRober

Latest Threads

Top