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

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

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/
 |
 |_adder.h
 |
 |_adder.c
 |
 |_/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.

 

adder.h

#ifndef ADDER_H
#define ADDER_H

int addNumbers(char number1, char number2);

#endif

 

adder.c

#include "adder.h"

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

 

main.c (Hello)

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

#include "../adder.h"

int main(void)
{
    printf("Hello " + addNumbers(2, 2));
    return 0;
}

main.c (Goodbye)

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

#include "../adder.h"

int main(void)
{
    printf("Goodbye " + addNumbers(2, 2));
    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): 

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

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/
 |
 |_adder.h
 |
 |_/Hello/
 |  |
 |  |_main.c
 |  |_adder.c
 |
 |_/Goodbye/
    |
    |_main.c
    |_adder.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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

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

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

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

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

 

Or that different toolchain versions were required.

 

 

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

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.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"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

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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

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. 

AS 7 makes a mess of my projects when I use "Add as link".  That's why I use AS 6.2.  This bug was on Atmel's todo list so maybe they fixed it.