Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Linker error when using Templated Class (Read 1592 times)
skc
Newbies
*
Offline


Posts: 8
Joined: Jan 20th, 2021
Linker error when using Templated Class
Jan 26th, 2021 at 10:36am
Print Post  
I am new to using Visual Micro for Arduino development, so this may be a bug of my own making.

Attached is a .zip of a simplified VS2019 solution "TemplateTest", with added libraries and shared code projects. It targets an Arduino Mega 2560 and is not complicated. I had to delete out some of the big files in the .vs folder to get it under attachment size limits. VS2019 seems to recreate them okay when you load up, so I don't think it's a big loss.

I have created a "Configuration" shared project which implements a templated class. It allows the storage and retrieval of a struct type 'T' from EEPROM. 

However, when I build this project with a simple "struct MegaConfig", the compile completes fine, but the linker complains -

Error linking for board ATmega2560 (Mega 2560) (Arduino Mega)
ccad8mtX.ltrans0.ltrans.o*: In function loop
Debug build failed for project 'TemplateTest'
   (.text+0xe78): undefined reference to ConfigurationClass<MegaConfig>::ConfigurationClass()
   (.text+0xe82): undefined reference to ConfigurationClass<MegaConfig>::~ConfigurationClass()
   (.text+0xeca): undefined reference to ConfigurationClass<MegaConfig>::Load()
 
collect2.exe*: error: ld returned 1 exit status


I've included the full output of the VM Build in the "Build Issue.txt" inside the solution folder.

For the life of me I cannot see where my bug is. So I suspect it is a Visual Micro bug?!

You will notice that the build is a Debug. But I have selected "Release" from the VS Configuration Manager dropdown, but it seems to have no effect of the VM build, which still does a Debug Build. I don't see anywhere else I can change this.
I am using an evaluation install, but you describe it as the full package but with a time-limit, so I don't expect it's that.

What have I run into here? A coding error or a VS2019/VM issue?

Thanks for any help you can give.

PS
I think your imposed caps limitation on your forum message preview window very childish. I had legitimate caps types in the cut'n'paste and I had to got through and change it. It's silly. It would be better if you just deleted idiot forum posts.

Thanks.

  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2708
Joined: Feb 13th, 2019
Re: Linker error when using Templated Class
Reply #1 - Jan 26th, 2021 at 11:24am
Print Post  
Thanks for the report and detail.

The same code (with the Shared project code moved into sketch\src\) also fails to compile in the Arduino IDE with the same error message, so it doesn't appear to be an issue with the build process.

Debug Config: In Release Build Configuration, Check on the vMicro > Debugger Menu that Debug is set to Off.


  
Back to top
IP Logged
 
skc
Newbies
*
Offline


Posts: 8
Joined: Jan 20th, 2021
Re: Linker error when using Templated Class
Reply #2 - Jan 26th, 2021 at 12:00pm
Print Post  
Thanks for the Debug Config tip. I see the correlation now. Though the linker error is still "Debug build failed for project 'MegaSAN'" i.e. "Debug", not "Release" even when I have "Release" selected on the VS2019 side, and can see the VM switch to Debug: OFF. So I don't know what is going on where.

I think a lot of my problem is that there are three facets to my environment now - VS, VM & Arduino-ID. And I am a bit confused as to what the responsibility of each actually is. But that's maybe just my learning curve.

So am I doing something illegal in my code? Or that is bad practice? I would have thought a templated class like this would be "basic" for a compiler/linker to deal with?!

Is this an issue I should take up with an Arduino forum somewhere? If so, do you have a link where I can best ask my question? 

Thanks very much for your prompt reply and help.

  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2708
Joined: Feb 13th, 2019
Re: Linker error when using Templated Class
Reply #3 - Jan 26th, 2021 at 4:00pm
Print Post  
Hopefully the below page helps to explain more about how the tools work together:
https://www.visualmicro.com/page/User-Guide.aspx?doc=How-The-Tools-Play-Together...

As far as I understand this is a C++ issue, and to seek help on the Arduino forum, a very simple example with the issue would need to be constructed (as simple as possible to yield the error).

The "Programming Questions" board should be the right place for this sort of query, and ensure you read the forum information about posting etc.. to ensure you get a response:
https://forum.arduino.cc/index.php?board=4.0
  
Back to top
IP Logged
 
skc
Newbies
*
Offline


Posts: 8
Joined: Jan 20th, 2021
Re: Linker error when using Templated Class
Reply #4 - Jan 27th, 2021 at 10:29am
Print Post  
Found the reason for my problem.

Its been a while since I used templates, and fell into the trap of thinking that including the .h is all there is to it. You need to include the .cpp (which #includes the template class .h) so that it pulls in the yet un-instantiated templated class module code.

It is only when you actually create a class instantiation with a substitute for 'T', is the compiler able to put together the templated class .h and .cpp into an actual class. From which you then can then instantiate a tangible instance variable.

Templates do not exist to the compiler until you supply T. Therefore the appearance of the template module "compiling" in the Output window means nothing. It only means something when 'T' is provided, and when you do that, the compiler needs to know everything about the template - its .h and .cpp. The compiler is not clever enough to find a template definition in some other file it previously "compiled" i.e. looked through. You have to provide it when you instantiate it by providing T.

Anyway, I have a working program. So it was nothing to do with VS2019, VM or Arduino-IDE. It was my coding!

I really need to read a good detailed book on C++11.

Thanks.

  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2708
Joined: Feb 13th, 2019
Re: Linker error when using Templated Class
Reply #5 - Jan 27th, 2021 at 11:03am
Print Post  
Thanks for the update, and the detailed explanation.
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint