nxp-mcuxpresso / vscode-for-mcux

Support for NXP Visual Studio Code for MCUXpresso Extension project.
13 stars 0 forks source link

Undefined reference functions #15

Closed MaxOvervoordee closed 9 months ago

MaxOvervoordee commented 9 months ago

Hi,

Ive been working on a motor FOC example in MCUXpresso for some time, and i'm trying to convert/transfer that code to the zephyr os. I was told as zephyr includes the global libraries of supported board, it should be doable. The way i'm doing that rn is just copy pasting library folders into zephyr and getting them in the directory using the method you suggest.

The problems tab shows zero errors, only unused variables which is fine.

The output tab shows a lot of undefinted reference which fails building the code. I've included the whole library with every path to every file folder, lib, with the next CMakeLists function: target_include_directories(). An example of some errors that are shown are the following: undefined reference to 'MID_Process_BL' This refers to this line or code: MID_Process_BL(&g_eMidCmd); And when clicking, visual studio code does show me the corresponding code in a file called "mid_sm_states.c".

Could anyone help me with this issue?

DerekSnell commented 9 months ago

Hi @MaxOvervoordee , An error like undefined reference to 'MID_Process_BL' means the linker cannot find the source with the function MID_Process_BL(). This likely means that source file is not included in the build.

The Cmake function target_include_directories() tells the compiler what include directories should be searched for header .h files.

But to include source .c files, you can use the Cmake function target_sources(), like this example in CMakeLists.txt.

I am not a Cmake expert. But when I want examples, I find it helpful to search the Zephyr repository for how the samples use Cmake. One method to easily search the repo, go to the web view of the Zephyr repo on GitHub, and use the search box at the top of the web page. Or grep works great as well. Using this method, here are some other references that may help.

You can create a list of sources for Cmake to include when building your application or library. See this example using list(APPEND) to add source files to a list. Then it calls target_sources(app PRIVATE ${app_sources}) to include all sources in the app_sources list in the build of the app.

Another Cmake command I use is FILE(GLOB), which allows you to include all *.c files in a folder in the build. Like this other example of CMakeLists.txt.

Let us know if those help. Best regards

MaxOvervoordee commented 9 months ago

Oh alright! I got it fixed with the next code:

`function(add_sources_from_directory target directory) file(GLOB_RECURSE source_files ${directory}/*.c) target_sources(${target} PRIVATE ${source_files}) endfunction()

add_sources_from_directory(app lib)`

With this, it searches for all .c files and adds them