0
$\begingroup$

Rosanswers logo

Hello. I'm having an issue linking a header to my code. I'm trying to include libi2c from "https://github.com/amaork/libi2c" to use with ROS. (Stand alone this all works). I have a node called keyboard_nav: In it I have 'include', 'launch', and 'src' folders. I have a test CPP file called i2c_xavierROS:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "i2c.h"

using namespace std;

int main(int argc, char **argv)
{

    unsigned char buf[6];
    size_t buf_size = sizeof(buf);

    int fd;
    I2CDevice device;
    const char *data = "15291021";

    /* First open i2c bus */
    if ((fd = i2c_open("/dev/i2c-8")) == -1)
    {
        perror("Open i2c bus error");
        return -1;
    }

    /* Fill i2c device struct */
    device.bus = fd;
    device.addr = 0x0f;
    device.tenbit = 0;
    device.delay = 10;
    device.flags = 0;
    device.page_bytes = 8;
    device.iaddr_bytes = 0; /* Set this to zero, and using i2c_ioctl_xxxx API will ignore chip internal address */

    /* Write data to i2c */
    if (i2c_ioctl_write(&device, 0x0, data, strlen(data)) != strlen(data))
   {  /* Error process */ }

    /* Read data from i2c */
    if (i2c_ioctl_read(&device, 0x0, buf, buf_size) != buf_size)
    {
      for (int i = 0; i < buf_size; i++) {

            buf[i] = 0xff - i;
    }

    }

    i2c_close(fd);
    printf("buffer is ");
    printf("%s\n", buf);
/*
    for (i = 0; i < buf_size; i++)
    {

      //scanf( "%d", &buf[i]);
      printf("%s\n", buf);
    }*/
    return 0;

}

My #include "i2c.h" and i2c.c are saved in the include folder. Here is the CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(keyboard_nav)

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)
#add_executable for demo.cpp

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES keyboard_nav
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)



include_directories(include ${catkin_INCLUDE_DIRS} )

add_executable(i2c_xavierROS src/i2c_xavierROS.cpp)
target_link_libraries(i2c_xavierROS ${catkin_LIBRARIES})
add_dependencies(i2c_xavierROS keyboard_nav_generate_messages_cpp)

When I run catkin_make I get this error:

CMakeFiles/i2c_xavierROS.dir/src/i2c_xavierROS.cpp.o: In function `main':
i2c_xavierROS.cpp:(.text+0x3a): undefined reference to `i2c_open'
i2c_xavierROS.cpp:(.text+0xaa): undefined reference to `i2c_ioctl_write'
i2c_xavierROS.cpp:(.text+0xd5): undefined reference to `i2c_ioctl_read'
i2c_xavierROS.cpp:(.text+0x112): undefined reference to `i2c_close'
collect2: error: ld returned 1 exit status
keyboard_nav/CMakeFiles/i2c_xavierROS.dir/build.make:113: recipe for target '/home/sp6david/henry_ws/devel/lib/keyboard_nav/i2c_xavierROS' failed
make[2]: *** [/home/sp6david/henry_ws/devel/lib/keyboard_nav/i2c_xavierROS] Error 1
CMakeFiles/Makefile2:872: recipe for target 'keyboard_nav/CMakeFiles/i2c_xavierROS.dir/all' failed
make[1]: *** [keyboard_nav/CMakeFiles/i2c_xavierROS.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j6 -l6" failed

I'd appreciate any help on fixing this.


Originally posted by MiniMe on ROS Answers with karma: 31 on 2019-11-25

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You need to either build i2c.c into a library (like they do in their Makefile, you could use add_library) and link against that library with your target_link_libraries call (probably what I'd recommend), or you need to also build the source of i2c.c into the executable that you are producing (might be a bit tricky with the fact that their source code is C not C++).

As it stands, the error you are seeing is because there is nothing in your CMakeLists.txt file even referencing the i2c.c source code.


Originally posted by jarvisschultz with karma: 9031 on 2019-11-25

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by MiniMe on 2019-11-25:
Thanks for the fast reply. I'll try this and see what comes of it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.