Visual Micro, using .cpp/.h, linker / build errors.

Hello, I have just got an Arduino and i am using visual micro because i like the VS ide.

I would rather use cpp and h files over ino so i know the layout and whats happening in my code, I have three files which are bellow, they are very basic just enough to get the errors.

main.ino

#include "test.h"

void setup() 
{
    Test.init(); 
}

void loop() { }

test.h

#ifndef _TEST_h
#define _TEST_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
#else
	#include "WProgram.h"
#endif

#include <SPI\\SPI.h>
#include <Ethernet\\Ethernet.h>

class TestClass
{
 protected:


 public:
	void init();
};

extern TestClass Test;

#endif

test.cpp

#include "Arduino.h"
#include "test.h"

void TestClass::init()
{
	byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
	Ethernet.begin(mac);
}

TestClass Test;

Here is my output log:

Compiling ‘main’ for ‘Arduino Uno’
test.cpp.o:In function TestClass::init()' test.cpp:undefined reference to Ethernet’
test.cpp:undefined reference to `Ethernet’
test.cpp:begin(unsigned char*)’
Error creating .elf

I am hoping to share some of the networking code i write between the Arduino and a windows project which is why i would rather use cpp/h for as much code as possible.

Thanks.

Are the Ethernet and SPI libraries in the linker and compiler path?

Why have you got two '\' in your #include statement? Surely you only need one (don't hold me to that)

Hello,

In case you have create or add a new library do a “reload tool chain” and a “Build>Clean Solution”

And also try this format for include you’re library

#include "SPI/SPI.h"

Thanks for the quick replies but none of those suggestions seem to have helped.
I have also just tried adding the folders to the VS c++ include paths and using “SPI.h” and “Ethernet.h” (and with <>) and the includes seem to have been removed from the solution, I am looking to see if they are set / imported from somewhere when i build.

Just to verify it is correct what you have done so far i will suggest you to make this with the arduino ide.

Try also to restart the vs after the "reload tool chain" and a "Build>Clean Solution"

I have tried both resetting VS after reloading the tool chain and i have tried making the same thing in the Arduino IDE, nothing seems to help. If i put the Ethernet code into the ino file it all works fine in both IDE's.

PhoenixNoble:
I have tried both resetting VS after reloading the tool chain and i have tried making the same thing in the Arduino IDE, nothing seems to help. If i put the Ethernet code into the ino file it all works fine in both IDE's.

Sorry my friend but i don't have any experience to make a library, so the the problem it looks like it is on include the other library in you're new library, maybe it is a command something like extern or whatever , i believe some one in the form with a better experience can help you to solve you're problem.

Best Regards,
Tasos

Hi,

For Arduino you must include libraries that you want to reference in cpp files in the [sketch].ino

You should also use "Project>Add/Import Sketch Library" to add the #includes until you understand the Arduino syntax.

Setting any VS C++ properties will not help because Visual Micro performs the same compile as the Arduino Ide which does not make use of any Visual Studio C++ properties.

Please also use the visualmicro.com form for support because this forum is for the Arduino Ide

Thanks

For what it is worth I had this same error and it was because my ino file had the same filename as my entry point cpp and h files.

For example, when I had AstroEQ.cpp, AstroEQ.h and AstroEQ.ino I got the unable to create .elf file error.

After I renamed AstroEQ.cpp and AstroEQ.h to AstroEQMain.cpp and AstroEQMain.h respectively then the VSS solution built correctly. Of course I also had to update any references to the header file throughout the rest of the code.

An alternative is to rename the ino file. But then you must also make sure that the containing directory is also renamed. It would seem that the directory and the ino must have the same name.

Hope this helps someone.