Call C++ function from c - Atmel studio 7

Go To Last Post
24 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have to call a C++ wrapper function from C, I am using Atmel Studio 7. I created three files as below in a gcc c/c++ executable project

 

wrapper.cpp

#include "wrapper.h"

using namespace std;

extern "C" int checker()
{
  //function definition
}

wrapper.h

#ifndef HEADER_FILE
#define HEADER_FILE

#ifdef __cplusplus

extern "C"
{

#endif

//declare functions here
  int checker();

#ifdef __cplusplus

}

#endif

#endif

main.c

#include <stdio.h>
#include "wrapper.h"

int main()
{
  printf("%d\n",checker());
  return 0;
}

When I build this project I get an error as "undefined reference checker". The same set of code works fine on Linux. Not sure what is the issue. I think the compiler is unable to define __cplusplus (just a guess not sure if that is the issue) Please help. 

Attachment(s): 

Last Edited: Tue. Feb 9, 2016 - 10:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PLEASE learn to READ the purpose of each forum before posting anything! Did you the very first post on top? https://www.avrfreaks.net/forum/d...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moved to General Programming

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

karanInterdigital wrote:
The same set of code works fine on Linux.

As you say - what you are attempting should work so it's something to do with how the code is being built so I think you need to post a complete AS7 project so others can look at what is going on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"undefined reference checker" is not necessarily the same as "undefined symbol checker".

What happens if checker is renamed pawn?

 

Once upon a time, I had trouble compiling a C program

because the compiler had implicitly #define-d kap.

I'd used kap because that was what I'd used in the Pascal program I was converting.

I'd used kap in the Pascal program because Pascal has its own use for cap.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have attached the project  GccApplication3.zip please check.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
As you say - what you are attempting should work

I'm not at a machine where I can do an experiment, but I disagree.

 

Why I think it won't work

When main is compiled this is done with the C compiler (and __cplusplus will not be defined). Thus the call will be coded into a "linker reference" to a "C decorated" function. When wrapper.cpp is compiled (__cplusplus will be defined, and) the actual function will get C++ decoration. Undecorated C symbols and C++ decorated symbols never meet/match.

 

What I think is a way to make it work

IIRC (again, not able to experiment/verify right now) you will need to do an extra jump "through" a C-decorated function (i.e. declared with extern "C") in a C++ file that in turn calls your wrapper. This will work because the extern "C" will make the "wrapper-wrapper" match the call from C code, but it's implementation can make use of C++ features since it resides in a C++ source file and is compiled  with the C++ compiler.

 

Facts, AFAIK/AFAIR

A C compiler produces C (un-)decorated symbols.

 

A C++ compiler produces C++-decorated symbols, unless you declare the symbol  extern "C"

 

A C++ compiler will produce C (un-)decorated symbols for anything declared  extern "C"

 

When a file is to be seen by both a C++ and a C compiler, any  extern "C" has to be hidden from the C compiler, which does not understand it. The  extern "C" construct is C++ only.

 

 

The extern "C" affects only the signature of a function.  Specifically, it does not imply that the body of the function is to be treated as C. If a function is in a C++ source file then it's body will be treated as C++, regardless of any extern "C" decorating it's signature.

 

 

Slightly aside re "it worked on Linux": Things must have been different in some way. The mechanism is the same "on Linux". It's the same compiler... The explanation as to why it worked there is possibly that the C++ compiler was used to compile all code (i.e. __cplusplus was defined for all compilation units). A few tests would make this clear, i.e. crash compilation if __cplusplus is not defined (#ifndef "__cplusplus"  ... #error "The C compiler is running!" or some such).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Wed. Feb 10, 2016 - 07:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
When wrapper.cpp is compiled (__cplusplus will be defined, and) the actual function will get C++ decoration.

Then I must be misunderstanding what:

extern "C" int checker()
{
  //function definition
}

will achieve? I thought this was going to give this function C (not C++ linkage) ?

 

Also do you remember the point when OP said: "The same set of code works fine on Linux. "? How did that happen then?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, it was I who misread it Cliff! (Can I blame the phone?)

 

I need to test it and experiment, but

i) am at work (and lunch soon ending), and

ii) don't have an avr-gcc - capable machine here

 

Perhaps tonight if I survive the do-or-die meeting this afternoon ;-)

 

Sorry for the confusion. If you read my "facts" section and map it onto the example it should work according to those facts.

 

@karanInterdigital: Appending the complete example, with all files, and a minimal example just demonstrating the problem and nothing else! That is EXCELLENT! You are one of a very small number of people who puts that work into a question here. You deserve all the help you can get, for your effort alone. But this also serves as a good example. I hope I survive the meeting and can look at your problem later today! ;-)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 if I survive the do-or-die meeting this afternoon

sad

 

(I've been to a few such meetings over the years. Never a good tim - even if I managed to scrape through unscathed most of the time. Probably just "flying under the radar").

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I survived. I'll try to find time and energy thiss evening to look at the project attached to the OP.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avr-nm *.o

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The results of a quick test with my AS7 installation:

 

1) Atmel Studio does not recognize the .cpp file extension. Change it to .C (Mark file in Solution Explorer, F2, type new name. Nota Bene! Capital 'C' in ".C").

 

2) Because Atmel Studio did not recognize the extension, it has set the Build Action for the file to "None". Change it to "Compile" (Click the file in the Solution Explorer, then in the Properties panel below the Solution Explorer change the Build Action in the combo box).

 

No, it will not be enough to just set the build action, and then leave the filename as wrapper.cpp . Other conventional extensions for C++ files, like .cxx also does not work.

 

3) The extern "C" should be used on the declaration in the header file only. (When you've fixed 1) and 2) above you will see the compiler emit an error for wrapper.C about expecting a left parenthesis before the string constant "C".)

 

Now do a Rebuild, and you will get only the expected warning about the variable a in main() not being used.

 

That's it.

 

I'm curious: Can someone with AS6 confirm if the .cpp file extension is not recognized there also?

 

No more from me re this right now. Good night!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

I'm curious: Can someone with AS6 confirm if the .cpp file extension is not recognized there also?

.cpp works in AS6 and it has worked for me everywhere I've used it.

 

I haven't tried AS7 but I'd be surprised if it didn't work there.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've never been fond of either .cpp or .C as C++ suffixes.

.cpp makes me think of the C preprocessor and .C is too close to .c .

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To be clear: I only experimented with the existing project setup attached in the OP. I did not actually experiment with adding a .cpp file myself - so it might be that the attacched project was "stained" in some way, and that a fresh added .cpp file will work.

 

I am positive that for the existing file in the attached project the .cpp extension did not work. Not a trace of a compilation attempt was seen in the build output with that extension on the file. 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I avoid using .C for C++ because the Windows operating system ignores case.  I don't have a problem with .cpp.  I've been using that for 20 years, ever since I learned C++.

 

Most of my software, and all my AVR software, is designed to be compiled with the Microsoft compiler as well as gcc.   If AS7 can't use .cpp, I would be stuck with AS6 forever.

 

Maybe the high priests of C++ should suggest a suffix that could be used by Windows, Linux and Apple.  I'm guessing .cpp may do the job.

 

For those who think pp stands for preprocessor, how about .c++?  That makes it pretty clear.  The plus sign is valid in Windows file names, but I don't know about Linux or Apple.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Linux takes + in files names.

Linux takes just about anything in file names, including spaces and non-printable characters.

 

I like .cc for C++ files, but .cpp seems to have conquered the world.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

compilers I work with use the .c or .cpp extension to choose how to launch the compiler.

Would be possible but not advise to be non-conventional.

 

There was a great debate about .h versus .hpp

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems Microsoft uses only .cpp and .cxx.  GCC allows .cxx also.  So if I had .cppophobia, I could use .cxx. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@JohanEkdahl  Thanks for the detailed explanation. I will try the solution you suggested and let you know. In the mean time I tried to do it using static library. I created a static c++ library for all the cpp files and added this library in my gcc C project. Added stdc++ library to the linker and it worked.

Last Edited: Fri. Feb 12, 2016 - 03:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[slaps forehead]

About the project you appended to your first post - did you create it as a C project or a C++ project? If it was created as a C project, then that could explain a lot. In fact, it could explain everything!

 

I'm sorry I didn't think about this right away.

[commencing 5-minute facepalm]

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is a GCC C executable project.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That explains why it won't compile a C++ file (unless you really force it as I showed above).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]