Difference between `zephyr_library_sources()` and `target_sources()` in CMakeLists.txt

Hello everyone,

I'm developing a demo to create my own driver using DeviceTree. And I defined the device in the driver source files.

I called the driver API in the `main.c`. But when I added the driver files to my project, there is an error when linking.

The linker says: `undefined reference to `__device_dts_ord_14'`.

It means that the linker can't find the device structure defined by the driver source.

But when I change my CMakeLists.txt, from `zephyr_library_sources()` to `target_sources(app )`,it was successfully built.

See:

 

# The config that causes link error
# zephyr_include_directories(.)
# zephyr_library()
# zephyr_library_sources_ifdef(CONFIG_JAYANT_VOLTAGE_SENSOR  voltage-sensor.c)
# zephyr_library_sources_ifdef(CONFIG_JAYANT_BLINK_LED       blink-led.c)


# The config that build successfully
target_include_directories(app PRIVATE .)
target_sources_ifdef(CONFIG_JAYANT_VOLTAGE_SENSOR app PRIVATE  voltage-sensor.c)
target_sources_ifdef(CONFIG_JAYANT_BLINK_LED      app PRIVATE  blink-led.c)

I want to know the difference between the 2 ways to add source files.

I learned the 1st way from the driver code in `zephyr/driver/`.  So I think it's a right way to add driver code to a project.

I have no ideas about why my own driver code can only be added as application.

Related