Topics

Adding own Library to project

König Patrick <Patrick.Koenig@...>
 

Hello,

 

My name is Patrick König. I am new to zephyr. I just wrote a couple of functions as additions to the zephyr/samples/subsys/usb/cdc_acm Sample Project for the Nordic nRf52840. These are working fine and have been tested.

 

Now I moved these functions from the main.c file to a custom module (.c and .h) within the src folder of my project. While doing so I managed to build the object file by keeping the sources in the source folder as described in the documentation.
Unfortunately I get errors from the linker telling me that I have undefined references to my functions.

 

So my question is: Is there a specific include process for adding custom modules to a project while using zephyr? Or did I miss a step in my process?

 

I have searched github for a similar issue, but I had no luck so far finding a solution, that’s why I hope to get an answer this way.

 

Thanks a lot

 

Patrick König

 

NewTec GmbH

 

System-Entwicklung und Beratung

 

Heinrich-von-Stephan-Str. 8

 

79100 Freiburg

 

 

 

Telefon: +49 76121117-42

 

Telefax: +49 76121117-99

 

email:   Patrick.Koenig@...

 

web:     www.newtec.de

 

 

 

------------------------------------------------------------------------------------------------------

 

Geschäftsführer: Johannes Werbach, Harald Molle, Ulrich Schwer, Michael Tröscher Registergericht Memmingen - HRB 7236  USt.-IdNr. DE130850199

 

------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

Patrick Boettcher
 

Hi Patrick,

On Wed, 13 Jun 2018 10:54:57 +0200
König Patrick <Patrick.Koenig@...> wrote:

Hello,

My name is Patrick König. I am new to zephyr. I just wrote a couple
of functions as additions to the zephyr/samples/subsys/usb/cdc_acm
Sample Project for the Nordic nRf52840. These are working fine and
have been tested.

Now I moved these functions from the main.c file to a custom module
(.c and .h) within the src folder of my project. While doing so I
managed to build the object file by keeping the sources in the source
folder as described in the documentation. Unfortunately I get errors
from the linker telling me that I have undefined references to my
functions.
The most convenient way of adding additional sources to your
application outside zephyr is using

target_sources(app PRIVATE src1.c src2.c)

in the CMakeLists.txt of your application (there where you are
including Zephyr's boilerplate.cmake).

I'm not sure whether that answers your question. Feel free to give some
more details and code-example if appropriate.

best regards,
--
Patrick.

Li, Jun R
 

Hi Patrick,

If you still want to keep your library (module), another way you can do is to add your library as the link dependency for the application library (app) by the following:

target_link_libraries(app your_lib_name)

Regards,
Jun


On 6/13/18, 06:37, "devel@... on behalf of Patrick Boettcher" <devel@... on behalf of patrick.boettcher@...> wrote:

Hi Patrick,

On Wed, 13 Jun 2018 10:54:57 +0200
König Patrick <Patrick.Koenig@...> wrote:

> Hello,
>
> My name is Patrick König. I am new to zephyr. I just wrote a couple
> of functions as additions to the zephyr/samples/subsys/usb/cdc_acm
> Sample Project for the Nordic nRf52840. These are working fine and
> have been tested.
>
> Now I moved these functions from the main.c file to a custom module
> (.c and .h) within the src folder of my project. While doing so I
> managed to build the object file by keeping the sources in the source
> folder as described in the documentation. Unfortunately I get errors
> from the linker telling me that I have undefined references to my
> functions.

The most convenient way of adding additional sources to your
application outside zephyr is using

target_sources(app PRIVATE src1.c src2.c)

in the CMakeLists.txt of your application (there where you are
including Zephyr's boilerplate.cmake).

I'm not sure whether that answers your question. Feel free to give some
more details and code-example if appropriate.

best regards,
--
Patrick.

Patrick Boettcher
 

On Wed, 13 Jun 2018 14:45:43 +0000
"Li, Jun R" <jun.r.li@...> wrote:

Hi Patrick,

If you still want to keep your library (module), another way you can
do is to add your library as the link dependency for the application
library (app) by the following:

target_link_libraries(app your_lib_name)
Well, this way of doing bears a whole of problems in some conditions.

While it works in probably most cases, including drivers in such a
library won't work.

Link-order sometimes messes up things as well if your library requires
zephyr functions from libraries where explicit dependencies was
not/could not be declared.

Sebastian is working on it, there is an issue on github's page for it.

--
Patrick.

Li, Jun R
 

That is true, this way doesn't support adding a driver module into the application since the app doesn't directly call anything from the driver. I'm wondering why driver modules in zephyr directory can be linked even without specifying dependency relationship. How is a driver module in Zephyr directory linked together?


On 6/13/18, 07:53, "devel@... on behalf of Patrick Boettcher" <devel@... on behalf of patrick.boettcher@...> wrote:

On Wed, 13 Jun 2018 14:45:43 +0000
"Li, Jun R" <jun.r.li@...> wrote:

> Hi Patrick,
>
> If you still want to keep your library (module), another way you can
> do is to add your library as the link dependency for the application
> library (app) by the following:
>
> target_link_libraries(app your_lib_name)

Well, this way of doing bears a whole of problems in some conditions.

While it works in probably most cases, including drivers in such a
library won't work.

Link-order sometimes messes up things as well if your library requires
zephyr functions from libraries where explicit dependencies was
not/could not be declared.

Sebastian is working on it, there is an issue on github's page for it.

--
Patrick.

König Patrick <Patrick.Koenig@...>
 

Thanks a lot for your help. I have tried your suggested solution with the CMakeList. Unfortunately, this did not work for me. I got the linker errors as described in my initial mail.

Let me try to illustrate my problem in a bit more detail:

If I add two function to my main.c file, for example usb_send() and usb_receive(), my code works fine. Once I move these functions to independent files like usb_xyz.c for the source and usb_xyz.h for the headers and include the header file in the main.c file, I am no longer able to link my project.

Usually I just need to include usb_xyz.h to my main.c file and add the usb_xyz.c and usb_xyz.h to a Makefile. With Cmake however, I added the usb_xyz.c file to the CMakelist and included header file in my main.c file, which leaves me with linker errors, like undefined reference to usb_send() and usb_receive().
I checked the compiler output, and saw the following line: “Building C object CMakeFiles/app.dir/src/usb_xyz.c.obj”, which leads me to believe that the file was compiled.

I hope this example makes my issue a bit clearer. Basically I just want to create modular Code that I can reuse in other zephyr projects as well.

Regards,

Patrick



-----Ursprüngliche Nachricht-----
Von: Li, Jun R [mailto:jun.r.li@...]
Gesendet: Mittwoch, 13. Juni 2018 16:59
An: Patrick Boettcher <patrick.boettcher@...>
Cc: König Patrick <Patrick.Koenig@...>; zephyr-devel@...; Sittkus Manuel <Manuel.Sittkus@...>
Betreff: Re: [Zephyr-devel] Adding own Library to project

That is true, this way doesn't support adding a driver module into the application since the app doesn't directly call anything from the driver. I'm wondering why driver modules in zephyr directory can be linked even without specifying dependency relationship. How is a driver module in Zephyr directory linked together?


On 6/13/18, 07:53, "devel@... on behalf of Patrick Boettcher" <devel@... on behalf of patrick.boettcher@...> wrote:

On Wed, 13 Jun 2018 14:45:43 +0000
"Li, Jun R" <jun.r.li@...> wrote:

> Hi Patrick,
>
> If you still want to keep your library (module), another way you can
> do is to add your library as the link dependency for the application
> library (app) by the following:
>
> target_link_libraries(app your_lib_name)

Well, this way of doing bears a whole of problems in some conditions.

While it works in probably most cases, including drivers in such a
library won't work.

Link-order sometimes messes up things as well if your library requires
zephyr functions from libraries where explicit dependencies was
not/could not be declared.

Sebastian is working on it, there is an issue on github's page for it.

--
Patrick.

Sebastian Boe
 

As far as I am able to understand your description, this is expected to work.

Perhaps you could share a minimal patch to an existing sample to demonstrate
when the linker error occurs.

________________________________________
From: devel@... <devel@...> on behalf of König Patrick <Patrick.Koenig@...>
Sent: Thursday, 14 June 2018 10:32:43 AM
To: Li, Jun R; Patrick Boettcher
Cc: zephyr-devel@...; Sittkus Manuel
Subject: Re: [Zephyr-devel] Adding own Library to project

Thanks a lot for your help. I have tried your suggested solution with the CMakeList. Unfortunately, this did not work for me. I got the linker errors as described in my initial mail.

Let me try to illustrate my problem in a bit more detail:

If I add two function to my main.c file, for example usb_send() and usb_receive(), my code works fine. Once I move these functions to independent files like usb_xyz.c for the source and usb_xyz.h for the headers and include the header file in the main.c file, I am no longer able to link my project.

Usually I just need to include usb_xyz.h to my main.c file and add the usb_xyz.c and usb_xyz.h to a Makefile. With Cmake however, I added the usb_xyz.c file to the CMakelist and included header file in my main.c file, which leaves me with linker errors, like undefined reference to usb_send() and usb_receive().
I checked the compiler output, and saw the following line: “Building C object CMakeFiles/app.dir/src/usb_xyz.c.obj”, which leads me to believe that the file was compiled.

I hope this example makes my issue a bit clearer. Basically I just want to create modular Code that I can reuse in other zephyr projects as well.

Regards,

Patrick



-----Ursprüngliche Nachricht-----
Von: Li, Jun R [mailto:jun.r.li@...]
Gesendet: Mittwoch, 13. Juni 2018 16:59
An: Patrick Boettcher <patrick.boettcher@...>
Cc: König Patrick <Patrick.Koenig@...>; zephyr-devel@...; Sittkus Manuel <Manuel.Sittkus@...>
Betreff: Re: [Zephyr-devel] Adding own Library to project

That is true, this way doesn't support adding a driver module into the application since the app doesn't directly call anything from the driver. I'm wondering why driver modules in zephyr directory can be linked even without specifying dependency relationship. How is a driver module in Zephyr directory linked together?


On 6/13/18, 07:53, "devel@... on behalf of Patrick Boettcher" <devel@... on behalf of patrick.boettcher@...> wrote:

On Wed, 13 Jun 2018 14:45:43 +0000
"Li, Jun R" <jun.r.li@...> wrote:

> Hi Patrick,
>
> If you still want to keep your library (module), another way you can
> do is to add your library as the link dependency for the application
> library (app) by the following:
>
> target_link_libraries(app your_lib_name)

Well, this way of doing bears a whole of problems in some conditions.

While it works in probably most cases, including drivers in such a
library won't work.

Link-order sometimes messes up things as well if your library requires
zephyr functions from libraries where explicit dependencies was
not/could not be declared.

Sebastian is working on it, there is an issue on github's page for it.

--
Patrick.

König Patrick <Patrick.Koenig@...>
 

I solved it. I made a mistake while moving some of the function to the separated module.
While preparing the example I noticed, that some of the functions in the USB CDC_ACM example, which I used as a base for my project, were declared as static, which I did not notice.
When I added them to my lib, I did not see the static keyword and I forgot to remove it. This functions gave me the errors as described.

I apologize for my mistake and I want to thank all of you for your help.

Patrick


-----Ursprüngliche Nachricht-----
Von: Bøe, Sebastian [mailto:Sebastian.Boe@...]
Gesendet: Donnerstag, 14. Juni 2018 14:39
An: König Patrick <Patrick.Koenig@...>; Li, Jun R <jun.r.li@...>; Patrick Boettcher <patrick.boettcher@...>
Cc: zephyr-devel@...; Sittkus Manuel <Manuel.Sittkus@...>
Betreff: Re: [Zephyr-devel] Adding own Library to project

As far as I am able to understand your description, this is expected to work.

Perhaps you could share a minimal patch to an existing sample to demonstrate when the linker error occurs.

________________________________________
From: devel@... <devel@...> on behalf of König Patrick <Patrick.Koenig@...>
Sent: Thursday, 14 June 2018 10:32:43 AM
To: Li, Jun R; Patrick Boettcher
Cc: zephyr-devel@...; Sittkus Manuel
Subject: Re: [Zephyr-devel] Adding own Library to project

Thanks a lot for your help. I have tried your suggested solution with the CMakeList. Unfortunately, this did not work for me. I got the linker errors as described in my initial mail.

Let me try to illustrate my problem in a bit more detail:

If I add two function to my main.c file, for example usb_send() and usb_receive(), my code works fine. Once I move these functions to independent files like usb_xyz.c for the source and usb_xyz.h for the headers and include the header file in the main.c file, I am no longer able to link my project.

Usually I just need to include usb_xyz.h to my main.c file and add the usb_xyz.c and usb_xyz.h to a Makefile. With Cmake however, I added the usb_xyz.c file to the CMakelist and included header file in my main.c file, which leaves me with linker errors, like undefined reference to usb_send() and usb_receive().
I checked the compiler output, and saw the following line: “Building C object CMakeFiles/app.dir/src/usb_xyz.c.obj”, which leads me to believe that the file was compiled.

I hope this example makes my issue a bit clearer. Basically I just want to create modular Code that I can reuse in other zephyr projects as well.

Regards,

Patrick



-----Ursprüngliche Nachricht-----
Von: Li, Jun R [mailto:jun.r.li@...]
Gesendet: Mittwoch, 13. Juni 2018 16:59
An: Patrick Boettcher <patrick.boettcher@...>
Cc: König Patrick <Patrick.Koenig@...>; zephyr-devel@...; Sittkus Manuel <Manuel.Sittkus@...>
Betreff: Re: [Zephyr-devel] Adding own Library to project

That is true, this way doesn't support adding a driver module into the application since the app doesn't directly call anything from the driver. I'm wondering why driver modules in zephyr directory can be linked even without specifying dependency relationship. How is a driver module in Zephyr directory linked together?


On 6/13/18, 07:53, "devel@... on behalf of Patrick Boettcher" <devel@... on behalf of patrick.boettcher@...> wrote:

On Wed, 13 Jun 2018 14:45:43 +0000
"Li, Jun R" <jun.r.li@...> wrote:

> Hi Patrick,
>
> If you still want to keep your library (module), another way you can
> do is to add your library as the link dependency for the application
> library (app) by the following:
>
> target_link_libraries(app your_lib_name)

Well, this way of doing bears a whole of problems in some conditions.

While it works in probably most cases, including drivers in such a
library won't work.

Link-order sometimes messes up things as well if your library requires
zephyr functions from libraries where explicit dependencies was
not/could not be declared.

Sebastian is working on it, there is an issue on github's page for it.

--
Patrick.