What is CMake equivalent for a prebuilt third-party library?

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Thu Oct 10, 2019 11:30 am

I am porting a make-based project to build using Cmake because I have updated to v4.x IDF. I know this may not be required, but Cmake seems to be favored by Espressif now. I have a third-party library (for a Bosch environmenal sensor).

The current make component.mk file has just this one line:
COMPONENT_ADD_LDFLAGS=$(COMPONENT_PATH)/libalgobsec.a

I am very new to Cmake and don't have a good idea what the equivalent should be in the CMakeLists.txt file.

Can someone tell me the syntax to use in my CMakeLists.txt file? Or at least what keywords I should be looking at?

Thanks!

ESP_Angus
Posts: 1877
Joined: Sun May 08, 2016 4:11 am

Re: What is CMake equivalent for a prebuilt third-party library?

Postby ESP_Angus » Thu Oct 10, 2019 11:39 pm

Hi jsam589,

You can do something like this in the component CMakeLists.txt file:

Code: Select all

target_link_libraries(${COMPONENT_LIB} "-L ${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${COMPONENT_LIB} algobsec)
EDIT: Changed COMPONENT_TARGET to COMPONENT_LIB.

The first line adds the component directory (which contains the binary library) to the linker search path, and the second line will populate the linker command line with "-lalgobsec".

If "libalgobsec" depends on some other libraries, add these after "algobsec" on the second invocation of "target_link_libraries".


Angus

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Fri Oct 11, 2019 2:11 am

I tried pasting those two lines in verbatim, but then the build failed and said this command is not scriptable.
Could there be a different version of the command, or something that would go inside idf_component_register(...) which would be allowed?
Thanks!

CMake Error at /home/jsam/ESP40/idf_40b1_z/esp-idf/tools/cmake/component.cmake:225 (message):
CMake Error at
/home/jsam/Inspect2nd/CompSnd2/components/bme_bsec_lib/CMakeLists.txt:3
(target_link_libraries):

target_link_libraries command is not scriptable

Call Stack (most recent call first):

ESP_Angus
Posts: 1877
Joined: Sun May 08, 2016 4:11 am

Re: What is CMake equivalent for a prebuilt third-party library?

Postby ESP_Angus » Fri Oct 11, 2019 3:57 am

Hi jsam,

Very sorry, I used the wrong variable name. Have edited the post.

This is cribbed from the esp_wifi component, which has to do the same thing (but in a more complex way):
https://github.com/espressif/esp-idf/bl ... ts.txt#L20

The PUBLIC keyword shown in the link is unnecessary in your case, I think. Also, make sure these two lines are placed after idf_component_register().

If it still doesn't work, could you please post the full CMakeLists.txt file you're using, and confirm the exact IDF version?

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Fri Oct 11, 2019 11:45 am

I tried update the CMakeLists.txt file but still getting errors, although slightly different now. It may be that this is totally not the correct way to handle a third-party pre-built library, but this is how we have been successfully doing it with 'make' and v3.x IDF.

My current IDF is v4.0-beta1, which obtained with "git clone -b v4.0-beta1 --recursive"

The 3 lines below are exactly what I currently am putting in the CMakeLists.txt. The component directory is named components/bme_bsec_lib/ and contains only library file libalgobsec.a and CMakeLists.txt.

idf_component_register()
target_link_libraries(${COMPONENT_LIB} "-L ${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${COMPONENT_LIB} algobsec)

Today's errors are:

CMake Error at components/bme_bsec_lib/CMakeLists.txt:2 (target_link_libraries):
INTERFACE library can only be used with the INTERFACE keyword of
target_link_libraries

CMake Error at components/bme_bsec_lib/CMakeLists.txt:3 (target_link_libraries):
INTERFACE library can only be used with the INTERFACE keyword of
target_link_libraries

ESP_Angus
Posts: 1877
Joined: Sun May 08, 2016 4:11 am

Re: What is CMake equivalent for a prebuilt third-party library?

Postby ESP_Angus » Mon Oct 14, 2019 3:28 am

Hi jsam589,

Sorry, I should have asked for the full details about the component before replying.

The root cause of this error is that the component doesn't have any source files (no "idf_register_component(SRCS ...)"). CMake calls this as an "interface" library target rather than a regular library target. We try to smooth over this distinction on the IDF side but when using CMake commands like target_link_libraries() then it needs to be followed.

Try this:

Code: Select all

idf_component_register()
target_link_libraries(${COMPONENT_LIB} INTERFACE "-L ${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${COMPONENT_LIB} INTERFACE algobsec)
You may need to explicitly tell CMake that algobsec depends on libc, libm, libgcc (assuming it calls some libc functions):

Code: Select all

idf_component_register()
target_link_libraries(${COMPONENT_LIB} INTERFACE "-L ${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${COMPONENT_LIB} INTERFACE algobsec c m gcc)
Similarly if the library has dependencies on other libraries in the build, you may need to add these as well.

Angus

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Mon Oct 14, 2019 10:47 am

It is building successfully now! Thanks for your efforts to find the correct syntax!

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Fri Oct 18, 2019 2:01 pm

I need a little more understanding of what is happening here. When I use the three lines as shown (IDF v4.0-beta1 and cmake), I don't get any build errors. However, I don't see any evidence that anything actually happened with the library. Under IDF 3.3 and make, I get a new library created, libbme_bsec_lib.a, in the build output directory. Building with IDF v4.0-beta1, I get the folder build/esp-idf/bme_bsec_lib/, but it only has make and cmake files in it. No library files or even soft link back to original library. How can I be sure anything is being performed? Thanks.

pinock
Posts: 7
Joined: Thu Oct 17, 2019 12:32 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby pinock » Fri Oct 18, 2019 8:31 pm

Hi buddy. I've been trying for three months. i don't get any results. Do you have any working examples?

https://esp32.com/viewtopic.php?f=13&t=12733

jsam589
Posts: 25
Joined: Sat Aug 17, 2019 9:31 pm

Re: What is CMake equivalent for a prebuilt third-party library?

Postby jsam589 » Mon Oct 21, 2019 7:15 pm

Hi @pinock,

I did get success with mine. I will try to help you with your issue. Give me a day or so and I will try to replicate with your library and hello world example. Question: Which version of IDF are you using? I assume you are trying to use CMake and CMakeLists.txt, correct?

Who is online

Users browsing this forum: Google [Bot] and 16 guests