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 Could not build project after adding *.h and *.c files (Read 3786 times)
JoeMcC
Junior Member
**
Offline


Posts: 27
Joined: Oct 30th, 2016
Could not build project after adding *.h and *.c files
Apr 2nd, 2017 at 1:50am
Print Post  
I have a ino project and tried to add a C file and its associated h file.
The project would not build. So I added the C file as a ino file and deleted the included h file.
The project built, downloaded succesfully to my surprise.

I would like to know why adding the *.c and *.h files did not work and how the the build worked without having
the h file included.

I come from a C background.

Thanks
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Could not build project after adding *.h and *.c files
Reply #1 - Apr 2nd, 2017 at 12:17pm
Print Post  
Hi,

There can be many valid reasons for build errors. When you see a build error it will be providing information that will try to give some clue as to the problem. It's important to post that information when asking questions about build problems.

There is also a verbose output facility on the vMicro>Compiler menu that provides additional output information during builds. That might also give you a clearer picture or some info to post here.

There is certainly a big difference between .cpp and .c files. .h files were probably not an issue for you.

So let us first know what error you could see, obviously we have no info about what code you put in your c/h files and what you had in an .ino file(s)
« Last Edit: Apr 2nd, 2017 at 12:18pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
JoeMcC
Junior Member
**
Offline


Posts: 27
Joined: Oct 30th, 2016
Re: Could not build project after adding *.h and *.c files
Reply #2 - Apr 2nd, 2017 at 3:08pm
Print Post  
I figured it out by lots of reading on the  Arduino forum.

Adding *.c and *h files to an arduino project you have to change the extension of the *.c file to *.cpp and place them in the same folder of the *.ino file. 

There is another way the files can be added and that is to again change your *.c to a *.cpp and put both *.cpp and *.h files in their own folder (the folder must be named exactly the same as the *.h and  *.c files) then place the folder as a subfolder under the ... Arduino\libraries\    folder. 

Now building the file will not produce the errors and it runs as it is supposed to.


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


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Could not build project after adding *.h and *.c files
Reply #3 - Apr 2nd, 2017 at 3:31pm
Print Post  
I see I didn't realise you didn't know how to add the files to a project, I thought you were having problems accessing your code from other cpp/ino code

In visual studio you have many ways to add c and h files to projects. you don't need to do any renaming. Try right clicking the project and take a look at the menu. also look at tool bar and top menus for additional "add item" options.

Normally for arduino you can add your sources into the project folder or any folder below a folder called \src

You can also create arduino libraries which follow the rules you have described except the .c name doesn't matter

If you add new libraries from outside the editor then click the Rescan button on the Visual Micro explorer or restart the ide to use the new libs. Alternatively in VS2015 and VS2017 you can add the code as shared project code or as a shared library. The library you created can easily also be a shared library without code change then you will find it easier to edit and extend the source code. Sharing-code-between-projects.aspx

« Last Edit: Apr 2nd, 2017 at 3:31pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
JoeMcC
Junior Member
**
Offline


Posts: 27
Joined: Oct 30th, 2016
Re: Could not build project after adding *.h and *.c files
Reply #4 - Apr 2nd, 2017 at 7:38pm
Print Post  
The files are located in the same folder as my *.ino file. I tried using "add existing item" and although added to
the project it did not build.

Then i put them in their own /src folder below the *.ino file and although it did add them to the project it also 
did not work.

Here are the errors. 
=============================
Compiling 'I2C' for 'Arduino/Genuino Uno'
Build folder: file:///C:/Users/All%20surface/AppData/Local/Temp/VMicroBuilds/I2C/uno
 
ccVqbBDf.ltrans0.ltrans.o*: In function main
ccVqbBDf.ltrans0.o*: (.text.startup+0xd0): undefined reference to i2c_init()
ccVqbBDf.ltrans0.o*: (.text.startup+0xd6): undefined reference to i2c_start(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0xe2): undefined reference to i2c_stop()
ccVqbBDf.ltrans0.o*: (.text.startup+0xf2): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0xfe): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x106): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x10e): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x116): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.ltrans.o*: ccVqbBDf.ltrans0.o:(.text.startup+0x11e): more undefined references to i2c_write(unsigned char) follow
 
ccVqbBDf.ltrans0.ltrans.o*: In function main
ccVqbBDf.ltrans0.o*: (.text.startup+0x144): undefined reference to i2c_rep_start(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x14a): undefined reference to i2c_write(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x150): undefined reference to i2c_rep_start(unsigned char)
ccVqbBDf.ltrans0.o*: (.text.startup+0x154): undefined reference to i2c_readAck()
ccVqbBDf.ltrans0.o*: (.text.startup+0x15c): undefined reference to i2c_readAck()
ccVqbBDf.ltrans0.o*: (.text.startup+0x164): undefined reference to i2c_readAck()
ccVqbBDf.ltrans0.o*: (.text.startup+0x16c): undefined reference to i2c_readAck()
ccVqbBDf.ltrans0.o*: (.text.startup+0x174): undefined reference to i2c_readAck()
ccVqbBDf.ltrans0.ltrans.o*: ccVqbBDf.ltrans0.o:(.text.startup+0x17c): more undefined references to i2c_readAck() follow
 
ccVqbBDf.ltrans0.ltrans.o*: In function main
ccVqbBDf.ltrans0.o*: (.text.startup+0x184): undefined reference to i2c_readNak()
 
collect2.exe*: error: ld returned 1 exit status

Error compiling for board Arduino/Genuino Uno
========================
  
Back to top
 
IP Logged
 
JoeMcC
Junior Member
**
Offline


Posts: 27
Joined: Oct 30th, 2016
Re: Could not build project after adding *.h and *.c files
Reply #5 - Apr 2nd, 2017 at 8:08pm
Print Post  
I had to add the #ifdef  between the prototypes in my *.h file to get it to build

#ifdef __cplusplus
extern "C" {
#endif

Func prototypes

#ifdef __cplusplus
}
#endif

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


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Could not build project after adding *.h and *.c files
Reply #6 - Apr 2nd, 2017 at 8:58pm
Print Post  
Yes that makes sense

Thanks
  
Back to top
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint