## Multi-project solution using different AVR types. They can share c code right?

8 posts / 0 new
Author
Message

Hello experts and thanks for reading this. I'm trying to upgrade from AVR Studio 4 and want to take advantage of the Atmel Studio (ala Visual Studio) 'Solution' concept in order to share code between 2 different AVRs that will be communicating with eachother.

I create a solution, "Hello" which does not include an external makefile. It creates its default project inside the soluction, also called "Hello". The directory structure is:

/Hello/
|
|
|
|_/Hello/
|  |
|  |_main.c
|
|_/Goodbye/
|
|_main.c

Hello will be the master. I add a project called "Goodbye" as the slave. adder.h and adder.c are in the main folder. In the solution explorer, they are shown in the "Solution Items" folder. I know that both projects should somehow be able to use the code in adder.h and adder.c.

#ifndef ADDER_H

#endif

#include "adder.h"

int addNumbers(char number1, char number2) {
return number1 + number2;
}

main.c (Hello)

#include <avr32/io.h>
#include <stdio.h>

int main(void)
{
return 0;
}

main.c (Goodbye)

#include <avr32/io.h>
#include <stdio.h>

int main(void)
{
return 0;
}

Compiling I get "undefined reference to addNumbers'" for both projects. I've spent way too much time stuck on this but I really want to do this the right way. What do I need to do to get the above solution to compile?

## Attachment(s):

As you've shown it adder.c and adder.h are not part of either project. Both need to have adder.c listed. It can be the same copy of adder.c because AS7 has "Add as link" but it has to be added to each project that uses it. That is:

/Hello/
|
|
|_/Hello/
|  |
|  |_main.c
|
|_/Goodbye/
|
|_main.c


BTW don't be fooled into thinking that the layout hierarchy you see in Studio Projects\Solutions necessarily matches the layout on the disk. For example your adder.* files might actually reside at P:\some\very\long\path\to\adderfiles but they will still just be shown as adder.c and adder.h in the Solution\Project view.

Oh and I am not a great fan of:

#include "../adder.h"

I would just use:

#include "adder.h"

and then provide a -I to the compiler (that is "Directories" in the Project properties) giving the name of the directory where that header file is located which could be as simple as ".." but could equally be "P:\some\very\long\path\to\adderfiles\". If for example that was the location of these file you wouldn't really want to the code to contain:

#include "P:\some\very\long\path\to\adderfiles\adder.h"`

would you?

(and yes I know / not \ in include paths!)

Last Edited: Fri. Jul 14, 2017 - 12:17 PM

Unrelated to the file structure, but the title says, "different AVR types".

I did try quite hard once to get Atmel Studio (v6, IIRC) to build the different Projects within a Solution for different target AVR types.

I failed. It seemed to be a limitation of the underlying Visual Studio.

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...

awneil wrote:
I failed.
Really? You can't set -mmcu per project?

It may have been that one project was AVR8, and the other AVR32.

Or that different toolchain versions were required.

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...

clawson wrote:
BTW don't be fooled into thinking that the layout hierarchy you see in Studio Projects\Solutions necessarily matches the layout on the disk. For example your adder.* files might actually reside at P:\some\very\long\path\to\adderfiles but they will still just be shown as adder.c and adder.h in the Solution\Project view.

Depends on how you vcreate your folder structure. IIRC, if you create a folder in Solution explorer it will be reflected in the file system structure.

awneil wrote:

I did try quite hard once to get Atmel Studio (v6, IIRC) to build the different Projects within a Solution for different target AVR types.

I failed. It seemed to be a limitation of the underlying Visual Studio.

[...]

It may have been that one project was AVR8, and the other AVR32.   Or that different toolchain versions were required.

As long as the code is architecture-agnostic (e.g. does not refer to specific registers or so) it should be quite possible to have a source file  added (as link) in two different projects and compiled with two different compilers (for AVR-8 in one project and AVR-32 in the other) or just different versions. (The key to the latter is to set up different toolchain "flavours" in Studio - these have been present in Studio since version 5 IIRC).

If the code refers to specific hardware like I/O registers that need to vary in different projects then it becomes trickier, but not impossible. Of-course, code specific for each hardware needs top be written.

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]

Thanks! I knew it had to be something simple. "Add as link" worked after adding both the .c and .h files to both projects. The actual code remains in one place and only needs to be edited once and changes are reflected in both projects, which is exactly what I was looking for.

BTW, searching for "Add as link" I found this post from a guy having the exact same problem.

Thanks again!

Edit: You only have the "Add as link" the .c file and not the .h.

Last Edited: Mon. Jul 17, 2017 - 12:19 AM