|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Cannot Mix C Code
I am trying to integrate C sources into the project (based on SimpleTemplate) but I am unable to call a C function from C++, since at runtime I receive an undefined reference error. I have tried to use both
/*C++ File*/ extern "C" { #include "cheader.h" } and /*cheader.h*/ extern "C" { void func(); } Any ideas? Could it be due to name mangling? Last edited by nabioullinr : 01-25-2010 at 08:31 PM. |
|
#2
|
|||
|
|||
|
Re: Cannot Mix C Code
Unless we see more of the source, there's not much more we can recommend. You can post relevant sources here or PM me for help.
Your extern "C" declarations look fine, but how are you compiling the C code? is it being run through g++ or gcc? If it's being compiled as C++ by the IDE but you're trying to reference it using extern "C", then yes, name mangling will surely be an issue. |
|
#3
|
|||
|
|||
|
Re: Cannot Mix C Code
Quote:
Code:
/* tiny_scheme_interpreter.h */ ... int scheme_init(scheme *sc); ... Code:
/* tiny_scheme_interpreter.c */
...
int scheme_init(scheme *sc) {
return scheme_init_custom_alloc(sc,malloc,free);
}
...
Code:
echo "building $@";%ccompilerprefix% $(TOOL_PATH)ccppc %DebugModeFlags% %ToolFlags% $(ADDED_CFLAGS) %Includes% $(ADDED_INCLUDES) -DCPU=$(CPU) -DTOOL_FAMILY=$(TOOL_FAMILY) -DTOOL=$(TOOL) -D_WRS_KERNEL $(DEFINES) -o %OutFile% -c %InFile% $(CC_ARCH_SPEC) -ansi -Wall -MD -MP -mlongcall Code:
echo "building $@";%cppcompilerprefix% $(TOOL_PATH)ccppc %DebugModeFlags% %ToolFlags% $(ADDED_CFLAGS) %Includes% $(ADDED_INCLUDES) -DCPU=$(CPU) -DTOOL_FAMILY=$(TOOL_FAMILY) -DTOOL=$(TOOL) -D_WRS_KERNEL $(DEFINES) -o %OutFile% -c %InFile% $(CC_ARCH_SPEC) -ansi -Wall -MD -MP -mlongcall Last edited by nabioullinr : 01-25-2010 at 09:49 PM. |
|
#4
|
|||||
|
|||||
|
Re: Cannot Mix C Code
Is there any particular reason you can't compile the C as C++ in this case?
|
|
#5
|
|||
|
|||
|
Re: Cannot Mix C Code
I remember when I first placed the tiny_scheme_interpreter source file it had a CPP extension, which I changed to C; when I first tried compiling it, there were errors which indicated that it was definitely being compiled as C; e.g. non-C style comments were shown as an error. It must be the scheme_init() call in the ScriptParser C++ code which is being mangled, in spite of extern "C".
|
|
#6
|
||||
|
||||
|
Re: Cannot Mix C Code
But if you compile everything as C++, the symbols should match up and the linker should have no problem. I'd focus on just getting it to compile as C++. I don't know why this should be too much of a problem. If it's some some stupid persistent project setting that you can't change or something, try copying the code into new .cpp files.
|
|
#7
|
|||||
|
|||||
|
Re: Cannot Mix C Code
Quote:
As to what the problem may be with your current extern "C" way, what is a "scheme *" type defined as? Is it a C type or a C++ class? Consult this FAQ for more information. |
|
#8
|
|||
|
|||
|
Re: Cannot Mix C Code
Thanks! It works now.
I did not recognize at first that the source file was compiled separately; I wanted to have .c in order to convey the fact that it is a C source file, rather than a C++ class. |
|
#9
|
|||
|
|||
|
Re: Cannot Mix C Code
I'm now facing the same issue, but this time I cannot merely rename the source file to .cpp because it contains numerous implicit type conversions which are considered errors in C++. Manual typecasting will probably take a week. Maybe I should try calling from inline asm to avoid name mangling issues, or simply rename the C functions to the mangled name...not elegant but this might work.
Last edited by nabioullinr : 02-09-2010 at 12:17 AM. |
|
#10
|
|||
|
|||
|
Re: Cannot Mix C Code
Or just put the header include inside an extern "C".
|
|
#11
|
|||
|
|||
|
Re: Cannot Mix C Code
That only works when the file has the extension .cpp (i.e., is compiled as C++).
|
|
#12
|
|||
|
|||
|
Re: Cannot Mix C Code
Exactly... so when you are including the C header file in a .cpp file, you include the header as extern "C".
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cannot deploy code | Patrick Chiang | FRC Control System | 18 | 03-16-2013 03:46 PM |
| Cannot Download Code | mandalorethx | C/C++ | 5 | 01-15-2010 12:44 PM |
| Cannot get Code to Download to cRIO | greekgod8591 | C/C++ | 25 | 03-09-2009 10:10 PM |
| Cannot Download Code to our Robot | jcvd | C/C++ | 9 | 01-27-2009 01:09 PM |