Thread: compile a foreign .c file

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    4

    compile a foreign .c file

    Hello,

    I am new to C programming and tried using the Simple Open EtherCAT library (SOEM) on a raspberry Pi. The language used is C.

    I pulled the github repository from here (GitHub - OpenEtherCATsociety/SOEM: Simple Open Source EtherCAT Master) on my raspberry Pi. Then I built it as described in the README.md. The file path on my raspberry is now: /home/raspberrypi/SOEM.

    I then connected an EtherCAT slave (to be precise, I connected 3: EK1100, EL1809 and EL2809) and then have run the simple_test executable (can be found in: /home/raspberrypi/SOEM/build/test/linux/simple_test) which worked fine. The output I got was without errors and the slave devices were detected.

    Then I tried to programm my first own script which should make use of the SOEM functionalities (such as reading out the slaves PDO data). In the folder SOEM there is a simple_test.c file, which can be modified. When compiled, this file should do the exact same thing as the executable simple_test (which was described previously).

    However, when I tried compiling the simple_test.c file using
    gcc simple_test.c -o simple_test
    in the terminal window, it throws an error saying:
    fatal error: osal.h: No such file or directory
    #include "osal.h"

    The error should come from the fact that the program to be compiled does not know where the associated .h files are located. These are mostly under home/raspberrypi/SOEM/soem (however there are other files such as .o files in a different location).

    My question is: How do I just manage to tell the program where the linked files are located? Do I have to specify the full file path every time I use the #include "..." command? If I do that, the compilation still throws errors because the needed functions are not found.

    Thank you in advance!
    Best regards,
    Tobias

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Did the installation of the SOEM library install any header files in the "/usr/local/include/" or "/usr/include/" directories, then you should include them as
    Code:
    #include <header.h>
    // Not as:
    #include "header.h"
    Otherwise, run your compile command as:
    Code:
    gcc -I /path/to/headers/ simple_test.c -o simple_test
    <header.h> indicates a predefined include path. "header.h" indicates the current directory by default, unless you sue the "-I /path/" gcc option.
    Last edited by rstanley; 05-04-2022 at 09:06 AM.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You need to tell gcc where the include files are with the -I flag:
    -I/path/to/include/files
    You need to tell gcc where the library files (.a or .so files) are with the -L flag:
    -L/path/to/library/code
    And you need to tell it which library file(s) to use with the -l (lowercase L) flag:
    -lLIBRARY_FILE_NAME_WITHOUT_EXTENSION

    So something like this (note that the -l flag goes last):
    gcc -o mycode -I/path/to/include/files -L/path/to/library/code mycode.c -llibrary
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    May 2022
    Posts
    4
    Quote Originally Posted by rstanley View Post
    Did the installation of the SOEM library install any header files in the "/usr/local/include/" or "/usr/include/" directories
    Thank you for your response! Also big thanks to john.c
    The installation didn't seem to have installed any header files in the directiories mentioned above. So I guess, I have to define the directory paths manually.

    Unfortunately, there are several .h files under different paths. For example, one is in the /home/raspberrypi/SOEM/osal folder, another is under /home/raspberrypi/SOEM/oshw, etc.


    Do I then have to manually specify all these paths when compiling?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Do I then have to manually specify all these paths when compiling?
    You could pass -I/home/raspberrypi/SOEM and then specify header files like:
    Code:
    #include "osal/whatever"
    #include "oshw/whatever"
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    May 2022
    Posts
    4
    @john.c

    I now tried the following code:

    gcc simple_test.c -o simple_test -I/home/raspberrypi/SOEM/osal -I/home/raspberrypi/SOEM/osal/linux -I/home/raspberrypi/SOEM/oshw/linux -L/home/raspberrypi/SOEM/osal/linux/osal.c -L/home/raspberrypi/SOEM/oshw/linux/oshw.c

    But this threw out the following error code:
    /usr/bin/ld: /tmp/cclThXAw.o: in function `simpletest':
    simple_test.c: (.text+0x34): undefined reference to `ec_init'
    /usr/bin/ld: simple_test.c: (.text+0x54): undefined reference to `ec_config_init'
    /usr/bin/ld: simple_test.c: (.text+0x7c): undefined reference to `ec_config_map'
    /usr/bin/ld: simple_test.c: (.text+0x80): undefined reference to `ec_configdc'
    /usr/bin/ld: simple_test.c: (.text+0x98): undefined reference to `ec_statecheck'
    /usr/bin/ld: simple_test.c: (.text+0x1b4): undefined reference to `ec_send_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x1bc): undefined reference to `ec_receive_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x1c4): undefined reference to `ec_writestate'
    /usr/bin/ld: simple_test.c: (.text+0x1d0): undefined reference to `ec_send_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x1d8): undefined reference to `ec_receive_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x1e8): undefined reference to `ec_statecheck'
    /usr/bin/ld: simple_test.c: (.text+0x240): undefined reference to `ec_send_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x248): undefined reference to `ec_receive_processdata'
    /usr/bin/ld: simple_test.c: (.text+0x340): undefined reference to `osal_usleep'
    /usr/bin/ld: simple_test.c: (.text+0x378): undefined reference to `ec_readstate'
    /usr/bin/ld: simple_test.c: (.text+0x444): undefined reference to `ec_ALstatuscode2string'
    /usr/bin/ld: simple_test.c: (.text+0x49c): undefined reference to `ec_writestate'
    /usr/bin/ld: simple_test.c: (.text+0x4b4): undefined reference to `ec_close'
    /usr/bin/ld: simple_test.c: (.text+0x4e4): undefined reference to `ec_slavecount'
    /usr/bin/ld: simple_test.c: (.text+0x4f8): undefined reference to `ec_slave'
    /usr/bin/ld: simple_test.c: (.text+0x4fc): undefined reference to `ec_group'
    /usr/bin/ld: simple_test.c: (.text+0x528): undefined reference to `ec_DCtime'
    /usr/bin/ld: /tmp/cclThXAw.o: in function `ecatcheck':
    simple_test.c: (.text+0x620): undefined reference to `ec_readstate'
    /usr/bin/ld: simple_test.c: (.text+0x74c): undefined reference to `ec_writestate'
    /usr/bin/ld: simple_test.c: (.text+0x7c8): undefined reference to `ec_writestate'
    /usr/bin/ld: simple_test.c: (.text+0x810): undefined reference to `ec_reconfig_slave'
    /usr/bin/ld: simple_test.c: (.text+0x8b0): undefined reference to `ec_statecheck'
    /usr/bin/ld: simple_test.c: (.text+0x99c): undefined reference to `ec_recover_slave'
    /usr/bin/ld: simple_test.c: (.text+0xa98): undefined reference to `osal_usleep'
    /usr/bin/ld: simple_test.c: (.text+0xab0): undefined reference to `ec_group'
    /usr/bin/ld: simple_test.c: (.text+0xab8): undefined reference to `ec_slave'
    /usr/bin/ld: simple_test.c: (.text+0xad4): undefined reference to `ec_slavecount'
    /usr/bin/ld: /tmp/cclThXAw.o: in function `main':
    simple_test.c: (.text+0xb18): undefined reference to `osal_thread_create'
    /usr/bin/ld: simple_test.c: (.text+0xb4c): undefined reference to `ec_find_adapters'
    /usr/bin/ld: simple_test.c: (.text+0xb8c): undefined reference to `ec_free_adapters'
    collect2: error: ld returned 1 exit status

    I guess I -L-flagged the wrong library files. But I don't have any .a or .so files, only .c.o files found under /home/raspberrypi/SOEM/build/CMakeFiles/soem.dir.
    Should I use these? Is compiling code any different with CMake?

  7. #7
    Registered User
    Join Date
    May 2022
    Posts
    4
    I have also found another folder under the following path:

    /home/raspberrypi/SOEM/build/test/linux/simple_test/CMakeFiles/simple_test.dir

    in this folder, there are several files like "DependInfo.cmake" or "depend.make", "depend.internal", etc.
    Could I make use of these to compile my code more easily?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    First, go back and insure that you compiled and installed SOEM correctly with no errors reported. Then examine the Tutorial.

    Good luck!

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Did you execute sudo make install from the build directory?
    If not, do so.
    Last edited by john.c; 05-05-2022 at 07:28 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Once you've installed it, you should refer to the header files in your source code like this:
    Code:
    #include <soem/osal.h>
    #include <soem/oshw.h>
    Then compile like this:
    Code:
    gcc yourprog.c -o yourprog -lsoem
    (Note that -l is a lowercase L)
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindNextFile & File names containing foreign characters
    By JeanClaude in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2009, 08:45 AM
  2. Foreign characters
    By gavio in forum C Programming
    Replies: 15
    Last Post: 08-28-2006, 10:08 PM
  3. Foreign Char. in c++
    By El-Scotto in forum C++ Programming
    Replies: 10
    Last Post: 12-20-2003, 12:06 AM
  4. Foreign Mathematical Words
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-15-2002, 11:14 AM
  5. Foreign Investments
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-22-2001, 01:39 PM

Tags for this Thread