Need Help adding a precompiled library CMAKE Torizon IDE Project

Hi,

I have a relatively simple c project from the Torizon Ide Extension CMAKE template in vscode.
I would like to either add a precompiled *.a library, or insert it directly into the project, compile it and link it to the main application.
(e.g. an asn1 lib)

I now need to know and understand how to get this lib into the container.

I suspect by editing the Dockerfile files.

For example, let’s say I have a libAsn1Scheme.a file and want to make it available to my main application.
I store the corresponding headers under asn1/include.

Where do I store the libAsn1Scheme.a and what changes do I have to make where?
Or is there a ready working example?

Best
Gerald

Hi @gerko ,

Generally speaking, if you want to add any file to a container you can include it when building the container image by using the ADD instruction in the Dockerfile:

However, I believe the CMake template from the extension already adds the entire project directory to the SDK container when building the application, so adding the .a file in the project directory should be enough.

Of course, you still need to adapt your CMakeLists.txt to use it during the compilation/linking process.

Best regards,
Lucas Akira

Thanks,

I’m not making any progress.
I need an example or specific instructions.

what I have done so far:
(this works in debug mode Local AMD64)

## CMakeLists.txt in top
cmake_minimum_required(VERSION 3.0.0)

project(RpmsgTtyTester VERSION 1.0 LANGUAGES CXX C)
add_subdirectory(src)
add_executable(RpmsgTtyTester src/main.c)

target_include_directories (RpmsgTtyTester PUBLIC HDLCSchemeAsn1)
target_link_libraries(RpmsgTtyTester  ${CMAKE_SOURCE_DIR}/HDLCSchemeAsn1.a)

set(CMAKE_SYSTEM_NAME Linux)

set_target_properties(RpmsgTtyTester PROPERTIES RUNTIME_OUTPUT_DIRECTORY "debug" ARCHIVE_OUTPUT_DIRECTORY "debug" LIBRARY_OUTPUT_DIRECTORY "debug")

my folder structure

.
├── build-arm64
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── debug
│   ├── Makefile
│   └── src
├── build-local
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── debug
│   ├── Makefile
│   └── src
├── CMakeLists.txt
├── docker-compose.yml
├── Dockerfile
├── Dockerfile.debug
├── Dockerfile.sdk
├── HDLCSchemeAsn1
│   ├── asn_application.h
...

...
│   └── xer_support.h
├── HDLCSchemeAsn1.a
├── includes
├── src
│   ├── CMakeLists.txt
│   └── main.c
├── TestLib
│   ├── CMakeLists.txt
│   ├── include
│   └── libPrintWas.c
└── torizonPackages.json
[ 50%] Linking C executable debug/RpmsgTtyTester
/usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: /app/HDLCSchemeAsn1.a(der_encoder.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: /app/HDLCSchemeAsn1.a(der_encoder.o): Relocations in generic ELF (EM: 62)
/usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: /app/HDLCSchemeAsn1.a: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/RpmsgTtyTester.dir/build.make:98: debug/RpmsgTtyTester] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/RpmsgTtyTester.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2

I find it strange that the program works ‘locally’ without any problems and in the arm64 version I get this error message. (error adding symbols: file in wrong format)

Greetings and a nice weekend!

Hi @gerko ,

The error message can mean that you’re trying to link a library that was compiled for a different architecture than what the cross-compiler generates e.g. trying to link a amd64/x86_64 library when the compiler generates aarch64 binaries.

If that’s the case then it would make sense that it works when building for your local machine, but it doesn’t when trying to build for arm64.

For which architecture was your static library .a file compiled?

Best regards,
Lucas Akira

Yes, that is the error in this case. Rookie mistake. :shushing_face:

  • I have now included the library sources in the project
  • Adjusted CMakeLists.txt’s until the task ‘build-debug-arm64’ ran successfully
  • after the first ‘Torizon ARMv8’ debug there was still an error message
  • now the program is running … yeaaah

In the ‘build-arm64’ folder you will find the *.a file, which you could probably also use for the ‘precompiled’ version.

The Dockerfile does not need to be touched.

Glad the problem was solved!

If you have other questions, please let us know.

Best regards,
Lucas Akira