0

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

steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • 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. – steeldriver Mar 17 '23 at 20:22
  • 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. – Simon Sudler Mar 17 '23 at 20:29
  • 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 Mar 20 '23 at 18:37
  • Sorry I can't figure out how to get the formatting right yet! – abbyLunasonde Mar 20 '23 at 18:42
  • @abbyLunasonde don't try to get the formatting right in comments - [edit] the information into your question instead please – steeldriver Mar 20 '23 at 18:56
  • Ok got it thanks! Updated the question – abbyLunasonde Mar 20 '23 at 20:24
  • 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) – steeldriver Mar 20 '23 at 20:38
  • 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? – abbyLunasonde Mar 22 '23 at 17:07

0 Answers0