Go to Post *gasp* Robotics? bad for your health? I refuse to accept that as an answer. - Kiwi_queen [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-01-2010, 20:19
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
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 : 25-01-2010 at 20:31.
Reply With Quote
  #2   Spotlight this post!  
Unread 25-01-2010, 21:04
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
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.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
Reply With Quote
  #3   Spotlight this post!  
Unread 25-01-2010, 21:44
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
Re: Cannot Mix C Code

Quote:
/* ScriptParser.cpp*/
#include <string>
#include <cstdio>
#include "ScriptParser.h"
extern "C" {
#include "tiny_scheme_interpreter.h"
}

bool ScriptParser::init(string initFile) {
FILE *fscript;

printf("Initializing TinyScheme...");
scheme_init(&sc);
puts("Done");
...
}
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);
}
...
I checked the build properties and found that .c and .cpp files are compiled with the same flags but a different variable/macro is used (%ccompilerprefix% and %cppcompilerprefix%) in the command.
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
I checked the console output and the function definitely seemed mangled...any ideas on howto change compilation to C?

Last edited by nabioullinr : 25-01-2010 at 21:49.
Reply With Quote
  #4   Spotlight this post!  
Unread 25-01-2010, 22:00
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,077
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Cannot Mix C Code

Is there any particular reason you can't compile the C as C++ in this case?
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2010, 07:14
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
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".
Reply With Quote
  #6   Spotlight this post!  
Unread 26-01-2010, 07:24
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
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.
__________________
Reply With Quote
  #7   Spotlight this post!  
Unread 26-01-2010, 07:46
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,077
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Cannot Mix C Code

Quote:
Originally Posted by slavik262 View Post
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.
It seems that this is the simplest solution to this problem. Usually, the C/C++ mixing nastiness is only an issue if you are using a large C code base and/or pre-compiled C library that you want to link with in a C++ application. Since it seems like there is only a single C-style function, just rename it to .cpp and the compiler will compile it as C++.

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.
Reply With Quote
  #8   Spotlight this post!  
Unread 27-01-2010, 13:12
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
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.
Reply With Quote
  #9   Spotlight this post!  
Unread 09-02-2010, 00:10
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
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 : 09-02-2010 at 00:17.
Reply With Quote
  #10   Spotlight this post!  
Unread 09-02-2010, 11:51
jhersh jhersh is offline
National Instruments
AKA: Joe Hershberger
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: May 2008
Rookie Year: 1997
Location: Austin, TX
Posts: 1,006
jhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond repute
Re: Cannot Mix C Code

Or just put the header include inside an extern "C".
Reply With Quote
  #11   Spotlight this post!  
Unread 09-02-2010, 15:24
nabioullinr nabioullinr is offline
Registered User
FRC #1073
 
Join Date: Jan 2010
Location: Hollis, NH
Posts: 17
nabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura aboutnabioullinr has a spectacular aura about
Re: Cannot Mix C Code

That only works when the file has the extension .cpp (i.e., is compiled as C++).
Reply With Quote
  #12   Spotlight this post!  
Unread 09-02-2010, 15:26
jhersh jhersh is offline
National Instruments
AKA: Joe Hershberger
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: May 2008
Rookie Year: 1997
Location: Austin, TX
Posts: 1,006
jhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond repute
Re: Cannot Mix C Code

Quote:
Originally Posted by nabioullinr View Post
That only works when the file has the extension .cpp (i.e., is compiled as C++).
Exactly... so when you are including the C header file in a .cpp file, you include the header as extern "C".
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot deploy code Patrick Chiang FRC Control System 18 16-03-2013 15:46
Cannot Download Code mandalorethx C/C++ 5 15-01-2010 12:44
Cannot get Code to Download to cRIO greekgod8591 C/C++ 25 09-03-2009 22:10
Cannot Download Code to our Robot jcvd C/C++ 9 27-01-2009 13:09


All times are GMT -5. The time now is 13:49.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi