Skip to content

Conversation

@ighvh
Copy link
Contributor

@ighvh ighvh commented Aug 10, 2023

When linking a shared object/executable, libraries have to be added after the object files and not before. Otherwise, symbols used in the object files are not resolved.

See https://forum.linuxcnc.org/9-installing-linuxcnc/41983-linuxcnc-ethercat-undefined-symbol-ecrt-slave-config-sdo?start=0

When linking a shared object/executable, libraries have to be added  after the object files and not before. Otherwise, symbols used in the object files are not resolved.

See https://forum.linuxcnc.org/9-installing-linuxcnc/41983-linuxcnc-ethercat-undefined-symbol-ecrt-slave-config-sdo?start=0
ighvh added a commit to ighvh/linuxcnc-ethercat that referenced this pull request Aug 11, 2023
@andypugh
Copy link
Collaborator

I don't have any EtherCAT hardware, so I will just have to trust you on this....

@ighvh
Copy link
Contributor Author

ighvh commented Aug 11, 2023

To test this, no hardware is needed. The point is that the lcec module likes to link against other libraries, for instance libethercat.
It should lool like this:

# ldd /usr/lib/linuxcnc/modules/lcec.so 
        linux-vdso.so.1 (0x00007ffc8dfba000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff15c747000)
        liblinuxcnchal.so.0 => /lib/liblinuxcnchal.so.0 (0x00007ff15c738000)
        libethercat.so.1 => /lib/x86_64-linux-gnu/libethercat.so.1 (0x00007ff15c72d000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff15c54c000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff15c862000)

But right now, without this patch and sittner/linuxcnc-ethercat#122, it looks like this:

# ldd src/lcec.so
        linux-vdso.so.1 (0x00007ffe8addf000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb402a10000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb40282f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb402b2b000)

When loading the module which is missing the link to libethercat, the symbol ecrt_slave_config_sdo() oviously cannot be found, because libethercat is not loaded.

The reason why it does not work with the current makefiles is the order of the linker arguments. Missing symbols are resolved left to right.
For example,

$ gcc -omyexe obj1.o obj2.o -lethercat

creates an executable myexe based on the compiled objects obj1 and obj2. Symbols which are not resolved yet (for example, ecrt_slave_config_sdo()) are looked up in libethercat. This dependency is stored in the executable and can be examined by ldd, as seen above.

If the order of the arguments is swapped

$ gcc -omyexe -lethercat obj1.o obj2.o

missing symbols are not looked up in libethercat and stay unresolved. This is the current case.

@gmoccapy gmoccapy merged commit 0635f35 into LinuxCNC:master Oct 7, 2023
scottlaird added a commit to linuxcnc-ethercat/linuxcnc-ethercat that referenced this pull request Dec 30, 2023
github-merge-queue bot pushed a commit to linuxcnc-ethercat/linuxcnc-ethercat that referenced this pull request Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants