Making a subdirectory for my library cause compiler error

Hi,
I have made a .zip library. when I open my example , an error occur.

libraries\libtest\test.cpp.o: In function `test()':

C:\Users\Administrator\Documents\Arduino\libraries\libtest/test.cpp:4: undefined reference to `fun_2()'

collect2.exe: error: ld returned 1 exit status

The function test() defined in C:\Users\Administrator\Documents\Arduino\libraries\libtest/test.cpp
and the fun_2() defined in C:\Users\Administrator\Documents\Arduino\libraries\libtest/mylib/test2.cpp

I Know that if test2.cpp moved to libtest/ , problem solved. But i do not know why error occur when i contribute a library,Maybe the test2.cpp not be compiled.

How to solve this problem? THX

The libtest tree is :

  • examples

  • mylib

  • test2.cpp

  • test2.h

  • test.cpp

  • test.h

Please state which Arduino IDE version you're using.
Please post the library and a simplified sketch that demonstrates the issue.

Since it's probably not much code I'd recommend you to post the content of each file in the forum message using code tags(</> button on the toolbar). Put each file's code in a separate code box and indicate the filename and path of that file. Also attach the library files and demonstration sketch, if you click the "Reply" or "Preview" buttons you will see a link appear "Attachments and other options". Preferrably this would be in a folder structure that allows installation via Sketch > Include Library > Add .ZIP Library and loading the sketch via File > Examples > libtest

This will make it easy for people to browse your code on the forum without downloading anything and also for us to install the library to actually run the code. That will give you the most chance of getting an answer to your question.

pert:
Please state which Arduino IDE version you're using.
Please post the library and a simplified sketch that demonstrates the issue.

Since it's probably not much code I'd recommend you to post the content of each file in the forum message using code tags(</> button on the toolbar). Put each file's code in a separate code box and indicate the filename and path of that file. Also attach the library files and demonstration sketch, if you click the "Reply" or "Preview" buttons you will see a link appear "Attachments and other options". Preferrably this would be in a folder structure that allows installation via Sketch > Include Library > Add .ZIP Library and loading the sketch via File > Examples > libtest

This will make it easy for people to browse your code on the forum without downloading anything and also for us to install the library to actually run the code. That will give you the most chance of getting an answer to your question.

Thanks for guide.
arduino version is 1.6.7
The way adding .ZIP via Sketch > Include Library > Add .ZIP
and loading the sketch via File > Examples > libtest

The directory has 4 files and folders : examples, mylib, test.cpp and test.h.

the test.cpp is :

#include "test.h"

void test(void) {
	fun_2();
}

the test.h is:

#ifndef _TEST_H
#define _TEST_H

#include "mylib/test2.h"

void test();

#endif

Directory mylib has test2.cpp and test2.h

the test2.cpp is:

#include "test2.h"

void fun_2(void) {
	;
}

the test2.h is:

#ifndef _TEST_2_H
#define _TEST_2_H
#include <Arduino.h>
void fun_2(void);

#endif

I want to contribute a library with some folders and those folders have some *.cpp. That is all, Thanks

libtest.zip (1.62 KB)

Excellent job providing a simple example of the issue, thanks! It's always difficult when someone presents 5000 lines of code with multiple 3rd party libraries I have to hunt down just so I can try to compile it.

I have managed to get it working(see attached file). I had to move the source code into a src subfolder of the library, this is known as the 1.5 library format(see Arduino IDE 1.5: Library specification · arduino/Arduino Wiki · GitHub). I also had to add a library.properties file so that the library with 1.5 folder structure can be recognized by the Arduino IDE. After doing that it compiles. I don't know exactly why this is.

As a side note, it's not a good practice to name the library folder differently from the included header filename. The reason is that library folders that match the included filename will take priority over ones that don't match so if they don't match there is a chance the IDE will include a different file than what was intended by the user. This is frequently encountered by people trying to use the Arduino-IRRemote library due to the Arduino IDE installation containing the file RobotIRremote/src/IRremote.h which ends up getting included instead of the Arduino-IRRemote library's file of the same name.

libtest.zip (1.57 KB)

The problem with the 1.5 library format is it's not backwards compatible with Arduino IDE 1.0.x(EDIT: actually 1.0.5-r2 and previous). Believe it or not there are sill people lagging behind 12 or more production releases of the IDE. If you want to put some of your library source files in a subfolder while maintaining backwards compatibility there is another option, but you can only use a subfolder named utility. I have attached another version of your library with this folder structure. It will compile in Arduino IDE 1.0.x as well as Arduino IDE 1.6.x.

libtest1.0.zip (1.23 KB)

Thanks for your patient guidance.

The first attach file you provided works well. But when I try to add a subfolder to contain test2.cpp and test2.h under utility unzip form the second attach file libtest1.0, the problem occur again. May IDE do not support this operation.
This is The first problem I come across and post it on Arduino Forum,Thanks for your tolerance of awkward programmer. Thanks a lot for your help.

libtest.zip (1.81 KB)

GangHuang:
when I try to add a subfolder to contain test2.cpp and test2.h under utility unzip form the second attach file libtest1.0, the problem occur again. May IDE do not support this operation.

That's correct. It appears you can only put them in the utility folder root. You can't put them in a subfolder of the utility folder.

Thanks. I have finished my library follow your guidance and it works well. But another question is coming.

After making zip package ,I found that i made a mistake in my library. So that I have to delete the library which has been include by Arduino IDE, I try to update my library with Arduino IDE Sketch>include library>manage librarys,it does not work.

It is good for a user to update the library if library has been fixed or added functions. I want to figure out how to update a library which has been include by Sketch > Include Library > Add .ZIP library Thanks.

Unfortunately that option only works for the initial installation of the library. If you want to update the library you need to do it manually. I agree that this should be possible since usually there will be multiple versions of a library released and it would be nice if the user could easily update.

Sketch>include library>manage libraries only allows updating Libraries included in the Library Manager index.