Problem with include

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

Hello,

 

I have a problem with .h files.

When I put in my main.c the #include "tft.h" I can't use any functions from tft.cpp because I have this Error when I try to call a function : undefined reference to `initQueue'.

But if in my tft.h I put #include tft.cpp I don't have any problem... (while in tft.cpp I put #include "tft.h")

 

I don't know how to makes that work correctely without the #include tft.cpp in the file tft.h

This topic has a solution.
Last Edited: Fri. May 21, 2021 - 11:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


tft.h

 

 

tft.cpp

 

 

With the comment line in the tft.h that don't work, I can't use my function on my main.c 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make sure that main.c, tft.h, tft.cpp are all in your project directory.

 

However you should create a C++ project rather than a C project.

The "tft.h" declarations need a conditional extern "C" { } guard.

 

Life is considerably simpler if you have an all C project or an all C++ project.   When you mix C and C++ you need to be careful.

 

Your PC has plenty of disk space.   So there is little harm in copying tft.h, tft.cpp to your project directory.

If you want to use a single pair of tft.* files for multiple projects,  you add the relevant include path to the Project Properties.

 

David.

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

Thank you man I just past my C++ in C and that works ! 

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

So it looks as if your tft.cpp file is regular C.   i.e. no classes, overloaded functions, inheritance, ...

 

In which case you can have an "all C" project.  By just renaming tft.cpp as tft.c

 

David.

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

Yeah it was all in C but I don't why it was in c++. I didn't create the project I just work on !

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

Simple answer.   If all the other project files are C++ renaming a C file to CPP avoids the "mix C and C++" linking issue.

 

On the other hand,  adding the conditional guards to "library" header files solves this problem.

 

Look at all your C system headers.    I suspect that they all contain the guards.   e.g. <stdio.h>, <stdlib.h>

 

David.

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

It was the only file in C++ other files is in C. I think it's because my company took some file from an other company to control a screen wink And before I come they work with Arduino so it was ok but now they pass to Atmel studio

Last Edited: Fri. May 21, 2021 - 12:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quite a lot of Arduino "standard functions" are just regular C.   e.g. map() or digitalWrite()

Likewise many C++ classes are a simple wrapper on top of C functions.   e.g. U8g2lib class

 

It is important to understand the linking problem.   Overloaded C++ functions require unique names for external linking.   This is known as "name mangling".

And obviously how to solve it.

 

David.

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

david.prentice wrote:
It is important to understand the linking problem.   Overloaded C++ functions require unique names for external linking.   This is known as "name mangling".

And obviously how to solve it.

+1

 

and note that this is a standard C/C++ issue - nothing specific to Atmel, Microchip, GCC, SAM, ARM, etc - so there is plenty of general material on the subject  ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for all your answers !