Ask Your Question

Issue with include: In function 'main': Undefined reference to

asked 2019-11-25 10:03:23 -0500

Hello. I'm having an issue linking a header to my code. I'm trying to include libi2c from "" 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;


    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)

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

#  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.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2019-11-25 10:43:32 -0500

updated 2019-11-25 10:44:21 -0500

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.

edit flag offensive delete link more


Thanks for the fast reply. I'll try this and see what comes of it.

MiniMe gravatar image MiniMe  ( 2019-11-25 14:02:17 -0500 )edit

answered 2019-11-29 05:45:31 -0500

updated 2019-11-29 05:49:14 -0500

I got this working with the target_link_libraries. Though there were many "tutorials" on how to do this they were over complicated and not clear. I eventually stumbled onto this YouTube video which was very helpful and I got the library linked with the addition of an extra CMakeLists inside the library.


edit flag offensive delete link more

answered 2019-11-25 12:00:20 -0500

jordan gravatar image


Try specifying the link directory. Example:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-11-25 10:03:23 -0500

Seen: 497 times

Last updated: Nov 29 '19