Score:0

Building C++ program with i2c-dev lib

lk flag

I have been trying to build a C++ program using the i2c-dev.h library on Ubuntu Core 22.04.02 LTS, so I can read and write I2C packets. But I can't get my program to build correctly. I tried including the path in my makefile but the compiler returns errors from what appears to be compiling some of the Linux headers themselves.

Here is my makefile now:

CC = g++
CFLAGS = -Wall
ALL_LIBS = -lrt -lstdc++ -std=c++11 -pthread -L/opt/picoscope/lib -lps4000a -libi2c
INCLUDES = -I/opt/picoscope/include/libps4000a -I/usr/src/linux-headers-5.15.0-67-generic/include -I/usr/src/linux-headers-5.15.0-67-generic/arch/x86/include/generated
ALL_TARGET = verde

all: $(ALL_TARGET)

$(ALL_TARGET): verde.cpp verde.h
    $(CC) -o $(ALL_TARGET) verde.cpp $(CFLAGS) $(ALL_LIBS) $(INCLUDES)

clean: 
    rm -f *.o
    rm -f $(ALL_TARGET)
    rm -f *.bin
    rm -f *.csv
    rm -f *.txt
    sudo rm -rf CPU*
    sudo rm -rf RF*

Here is the latest error message when running make:

In file included from /usr/include/linux/posix_types.h:5,
                 from /usr/src/linux-headers-5.15.0-1032-realtime/include/uapi/linux/types.h:14,
                 from /usr/src/linux-headers-5.15.0-1032-realtime/include/linux/types.h:6,
                 from /usr/src/linux-headers-5.15.0-1032-realtime/include/uapi/linux/i2c-dev.h:12,
                 from /usr/src/linux-headers-5.15.0-1032-realtime/include/linux/i2c-dev.h:12,
                 from verde.h:54,
                 from verde.cpp:28:
verde.cpp: In function ‘void loggingSwitchDetect()’:
verde.cpp:929:36: error: invalid conversion from ‘void*’ to ‘char**’ [-fpermissive]
  929 |         dioValInt = strtol(dioVal, NULL, 16);
      |                                    ^~~~
      |                                    |
      |                                    void*
In file included from /usr/include/c++/11/cstdlib:75,
                 from verde.h:33,
                 from verde.cpp:28:
/usr/include/stdlib.h:178:43: note:   initializing argument 2 of ‘long int strtol(const char*, char**, int)’
  178 |                         char **__restrict __endptr, int __base)
      |                         ~~~~~~~~~~~~~~~~~~^~~~~~~~

enter image description here

hr flag
I notice you appear to have i2c in your home directory - is there a particular reason why you are not using the Ubuntu libi2c-dev package (which would install in the systemwide /usr/include and /usr/lib directories)? That is specifically described as a "userspace I2C programming library". Yay Picoscope btw.
Simon Sudler avatar
us flag
There could be any number of reasons for this... the `asm/rwonce.h` is located in the `x86` generated header section. Try `-I .../arch/x86/include/generated`. It is hard to guess without seeing the actual code.
abbyLunasonde avatar
lk flag
OK, I was using a locally built version because I pulled it from GitHub and realized it was different than the native Linux I2C libraries. Now I'm trying to use the native libs. Adding the .../arch/... dir helps, as well as using the 5.15.0-67 version of the Linux headers instead of the 5.15.0-1032. But now I'm getting errors using NULL!
abbyLunasonde avatar
lk flag
Sorry I can't figure out how to get the formatting right yet!
hr flag
@abbyLunasonde don't try to get the formatting right in comments - [edit] the information into your question instead please
abbyLunasonde avatar
lk flag
Ok got it thanks! Updated the question
hr flag
Thanks - but it's going to be hard to help without knowing **what** you are trying to build - is it some kind of kernel module, or is it a user-space application? If the latter, then AFAIK you really shouldn't be directly including kernel headers - see for example [What is kernel headers that can be used in userspace? Do their signature or interface differ than the headers in different directories?](https://unix.stackexchange.com/a/577005/65304)
abbyLunasonde avatar
lk flag
It is a user-space application. OK, I was just trying to implement an I2C interface to read and write to a bus. I actually figured out an alternative by calling the i2c-tools package through system commands. But ideally , for speed, I think I'd want to know how to use the I2C libs themselves. Any suggestions?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.