When i use bison & flex with vc6, i got got below errors
lex.yy.c(395) : error C2146: syntax error : missing ';' before identifier 'YY_PROTO'
lex.yy.c(395) : fatal error C1004: unexpected end of file found
what would be the cause for this??
please help.
Copied from Comment:
#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap YY_PROTO(( void ));
#else
extern int yywrap YY_PROTO(( void ));
#endif
#endif
The YY_PROTO macro is only to support old pre-standard C without support for prototypes. You will have hard to find a compiler that does not support that today. That means that as a first debugging step you could try to remove it completely since you want to use prototypes, i.e. modify lex.yy.c to the following:
#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap ( void );
#else
extern int yywrap ( void );
#endif
#endif
I know that lex.yy.c is a generated file, so that will not be a permanent fix, but it should at least confirm that the problem is related to the definition of YY_PROTO.
YY_PROTO is a macro that is defined earlier in the same file, so something odd is going on near the macro definition. Search earlier in the file to see how YY_PROTO is defined -- if its not getting defined, your compiler is doing something very weird.
Related
I have a cpp code in which I want to call a c function.
Both compile well to .o files, but when the clang++ is executing for compilation, I receive the following error:
file.cpp:74:12: error: expected unqualified-id
extern "C"
^
The code in the cpp file is the following:
void parseExtern(QString str)
{
#ifdef __cplusplus
extern "C"
{
#endif
function_in_C(str);
#ifdef __cplusplus
}
#endif
}
How can I avoid the error ? I can't compile the c file with clang++, I really need to use extern. Thanks.
The extern "C" linkage specification is something you attach to a function declaration. You don't put it at the call site.
In your case, you'd put the following in a header file:
#ifdef __cplusplus
extern "C"
{
#endif
void function_in_C(char const *); /* insert correct prototype */
/* add other C function prototypes here if needed */
#ifdef __cplusplus
}
#endif
Then in your C++ code, you just call it like any other function. No extra decoration required.
char const * data = ...;
function_in_C(data);
I've got a .h file and three .cpp files. All cpp files include the .h file.
I want to create a useless function in one of the cpp files and use alias in the other cpp files to refere to this useless function. But if i move the function from important.cpp to useless.cpp it does not want to compile anymore. For some reason it doesnt see the function, even if its declared in the header.
useless.h
#ifdef __cplusplus
extern "C" {
#endif
extern void useless(void);
#ifdef __cplusplus
}
#endif
useless.cpp
#ifdef __cplusplus
extern "C" {
#endif
void useless(void) { }
#ifdef __cplusplus
}
#endif
important.cpp
void important(void) __attribute__((alias("useless")));
error: 'void important()' aliased to undefined symbol 'useless'
void important(void) attribute((alias("useless")));
Why are you using a bunch of extern "C" stuff?
If it's a C++ project and a global C++ function, you really should claim it's a C function. Doing so can lead to undefined reference-type errors, as the symbol names are different for C and C++.
What you're trying to do is probably impossible. Take a look at gcc docs https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html It says:
It is an error if â__fâ is not defined in the same translation unit.
So if you remove useless from the translation unit where important is it looks like an error to me.
i have header file as follows
**something.h**
#ifdef __cplusplus
extern "C" {
#endif
blah
blah
extern CONST oMenu_t const menu[];
blah
blah
#ifdef __cplusplus
}
#endif
though i have used appropriate(i suppose) extern "C" still facing error "type qualifier specified more than once" while compiling with c++ compiler..
help please
multiple const is not needed
you may use:
extern oMenu_t const menu[];
in your .h file.
However, you must have something like below in one .c file (define only once, declare as many times, as used in .h)
oMenu_t const menu[] = {....}; // appropriate initializer
Add
#ifndef __FILENAME_HEADER_FILE
#define __FILENAME_HEADER_FILE
your existing head file goes here...
#endif // __FILENAME_HEADER_FILE
around your header file, this means you can include the .H many times but the compiler only sees it once.
FYI: Replace FILENAME with the name of your filename, the #define this needs to be different in each header file you use it.
I'm trying to call a C function defined in sample_c.c (with function declaration in sample_c.h) from a C++ code. I'm using this declaration in sample_c.h
extern "C" void print_c(void);
and this definition in sample_c.c
extern void print_c(void) {....}
and trying to generate lib which I want to link with my cpp code. I'm getting an error when trying to generate lib out of this C code.
gcc -c sample_c.c
error: expected identifier or â(â before string constant
I'm unable to rectify this. Can someone suggest where am I going wrong.
extern "C" is not valid in C. To write a header that can be used for both C and C++ you have to make sure that the extern "C" is not visible when compiled as C:
#ifdef __cplusplus
extern "C"
#endif
void print_c(void);
I'm using VS2008. I'm getting the following error.
BUILD: [02:0000000295:ERRORE] c:\wince700\platform\am33x_bsp\src\bootloader\bootpart\bootpart_e.cpp(61) : error C2732: linkage specification contradicts earlier specification for 'SdhcInitialize' {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(103)"}
BUILD: [02:0000000297:ERRORE] NMAKE : fatal error U1077: 'C:\WINCE700\sdk\bin\i386\ARM\cl.EXE' : return code '0x2' {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(104)"}
BUILD: [02:0000000299:ERRORE] clean TargetCompilePass -nologo BUILDMSG=Stop. BUILDROOT=C:\WINCE700\platform\AM33X_BSP CLEANBUILD=1 NOLINK=1 NOPASS0=1 failed - rc = 2. {log="C:\WINCE700\platform\AM33X_BSP\SRC\BOOTLOADER\bldsys.log(105)"}
file_1.cpp
extern "C"
{
// some extern declarations
extern void SdhcInitialize(DWORD slot);
}
file_2.c
void SdhcInitialize(DWORD slot)
{
//some code
}
Please guide me how to resolve.
I'm guessing that you have a header that contains a prototype for the SdhcInitialize() function, and that the header was written for use by C programs. So for example, the header file might include something like the following line:
SD_API_STATUS SdhcInitialize(DWORD slot);
without being enclosed in an extern "C" {} block (since the header is intended for C programs).
Additionally, I suspect that this header is being included - directly or indirectly - by file_1.cpp
This means that the header cannot be included in a C++ program without some additional work being done, otherwise the C++ program will see the declaration as meaning that SdhcInitialize() has C++ linkage.
You have two reasonable approaches to fixing this:
if you can modify the header, add the following lines around the declarations in the header:
#if __cplusplus
extern "C" {
#endif
// declarations go here
#if __cplusplus
}
#endif
This way, C++ files will have the declarations enclosed in a extern "C" linkage block, while C program will not see the extern "C" bits (which would otherwise confuse the C compiler).
I think an argument can be made that all C headers should include something like those lines so that the C functions can be consumed by C++ programs without hassle.
if you cannot modify the header for some reason, you can work around the problem by including the header in C++ files like so:
extern "C" {
#include "Sdhc-header.h"
}
If you surround a set of function declaration by extern "C" { ... }, you don't need to use an additionnal externkeyword in front of the function identifier.
extern "C"
{
// some extern declarations
SD_API_STATUS SdhcInitialize(DWORD slot);
}
When you try to include the "some header files of C" file in "C++ file"(the header file has some where extern "C" for some functions).
include the header earlier will solve the problem.
e.g. Try to move #include "myHeader.h" on the top lines of your C++ file.
This solves my problems.
Hope it helps....
I have solved this as follows (the other solutions did not work for me):
In the file vector.cc:
#define __INVECTOR //solves
#include "vector.h"
In vector.h:
#ifndef __INVECTOR
void function(...etc..);
#endif
This way the declaration isn't read unless we want to call the function from a different file.