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:

 

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.