Has anybody tried using the i2c_smbus_write_byte or any similar function on Raspberry Pi 4?
I can't get it compile it fails at the linking with not finding it.
I'm using it as described here: http://synfare.com/599N105E/hwdocs/rpi/rpii2c.html
All the headers recommended are there is and also the -li2c in the Makefile.
Can anybody tell what the problem can be? I have no clue at the moment.
Might be worth checking to see if libi2c-dev is present on your system.
sudo apt-get install libi2c-dev
may be all that you need.
The page you are linking to says:
With the Buster version, as of june 2019, the necessary details for
using i2c_smbus_write_byte_data() and siblings, require the following
include statements:
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
Using fgrep you can confirm that the function is declared in the /usr/include/i2c/smbus.h:
# cd /usr/include; fgrep -R i2c_smbus_write_byte *
i2c/smbus.h:extern __s32 i2c_smbus_write_byte(int file, __u8 value);
i2c/smbus.h:extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
So this should work:
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
int main(void) {
int i2c = open("/dev/i2c-1", O_RDWR);
i2c_smbus_write_byte(i2c, 1);
close(i2c);
return 0;
}
I tested that this example compiles successfully in the latest Raspbian Buster Lite:
gcc test.c -otest -li2c
If you are using g++ instead of gcc, then you should wrap the include directives with extern "C":
extern "C" {
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}
Related
I'm trying to use the memfd_create syscall in my C code. I tried to include sys/memfd.h as the man page for memfd_create says is appropriate, but GCC gives me an error "sys/memfd: No such file or directory".
I've tried Googling around and couldn't find anyone having the same problem. I noticed some versions of the manpage for memfd_create say that I should include sys/mman.h, but it didn't seem to help when I tried it. It would say memfd_create was implicitly declared.
Here is a minimal reproduction of my problem.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/memfd.h>
int main(){
int fd;
fd = memfd_create("test", MFD_CLOEXEC);
return 0;
}
I expect the above code to compile and run without error.
On older systems, you'll have to include linux/memfd.h for the MFD_ defines, and call memfd_create() via the the syscall(2) wrapper (and include unistd.h and sys/syscall.h for it work).
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/memfd.h>
#include <err.h>
int main(void){
int fd;
if((fd = syscall(SYS_memfd_create, "test", MFD_CLOEXEC)) == -1)
err(1, "memfd_create");
return 0;
}
The Ubuntu man-pages in Bionic (18.04) are not up to date with this API (including its implementation in Bionic).
The Focal man-page correctly shows how to include memfd_create(). It says:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sys/mman.h>
So you only need to include <sys/mman.h>, and you need to build with -D_GNU_SOURCE in your compiler flags. Or, do as the man page says and literally #define _GNU_SOURCE before including the header. However, I recommend just compiling with -D_GNU_SOURCE instead.
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin18.0.0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _GNU_SOURCE
#define __USE_GNU 1
#include <fcntl.h>
int main()
{
int fd = open("./test.txt", O_WRONLY|O_DIRECT);
close(fd);
return 0;
}
I use clang -o out test.c and get the following result:
test.c:14:39: error: use of undeclared identifier 'O_DIRECT'
int fd = open("./test.txt", O_WRONLY|O_DIRECT);
How can I solve the problem?
Thank you.
With this snippet it is impossible to tell what you are trying to do, but in general do not use nonstandard stuff in applications intended to be portable.
The portable way to accomplish your task is probably fsync.
To sum up
For LINUX, the O_DIRECT flag has to be included. For Mac OSX, O_DIRECT is not available. Instead, fcntl(fd, F_NOCACHE, 1) looks to be the canonical solution where fd is the file descriptor of the file. For Windows, there is a flag called FILE_FLAG_NO_BUFFERING as the counterpart in Windows of O_DIRECT
Reference here
I've been trying to get cgraph (https://graphviz.gitlab.io/_pages/pdf/cgraph.pdf) working so I read and write some graph files. I tried writing some very basic code:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/cgraph.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.dot", "w+");
if (fp == NULL) {
return -1;
}
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
agwrite(g, fp);
fclose(fp);
system("pause");
return 0;
}
What should be happening is that the file should be written to test.dot. This code works perfectly fine on Win64 release, but fails on Win64 debug, Win32 debug, and Win32 release. I have double checked the .lib files and .dll files settings in visual studio and in the file directories, making sure to copy the release and debug versions of each platform correctly. However, the agwrite keeps causing a "Microsoft Visual Studio C Runtime Library has detected a fatal error" crash on Win64 debug, Win32 debug, and Win32 release. The weird thing is if I change
agwrite(g, fp); to agwrite(g, stdout);, the code works on all platforms/configurations. I am so confused why this is happening. Here is the source file which contains the code for agwrite if that helps: https://github.com/ellson/MOTHBALLED-graphviz/blob/master/lib/cgraph/write.c
I cannot debug the issue because the source has been compiled into .dlls, and .libs for each platform/configuration.
I appreciate any suggestions/feedback,
Thank you
Edit:
For anyone godly enough to try and get this working on their own system, here are all my binaries, libs, and include files: https://www.dropbox.com/sh/o9tjz7txu4m0k5q/AAAnp8Wu99q9IsFN7kvqZP7Ta?dl=0
Edit 2:
The compiler I am using is MSVC 14 on Windows 10.
I found out that using cgraph directly results in an error when trying to use agwrite(). The solution is to use the GVC abstraction layer which comes with the Graphviz C API to do file I/O. Here is the code that worked:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/gvc.h>
int main() {
GVC_t *gvc;
gvc = gvContext();
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, "dot", "test.dot");
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
system("pause");
return 0;
}
Edit:
Here is the documentation for GVC: https://graphviz.gitlab.io/_pages/pdf/gvc.3.pdf
The reason of crashing is described on official Graphviz site:
This usually happens when the Graphviz library is built using one version of the stdio library, and the user’s program is compiled using another. If the FILE structure of stdio is different, the call to agread() will cause a crash. This is mainly a problem on Windows where we just provide a binary release built with one version of Visual Studio and stdio changes depending on the version of Visual Studio. It can also occur if the user tries to use cygwin or something similar which may also use an incompatible stdio.
https://graphviz.org/faq/#FaqAgreadCrash
Given this code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <pthread.h>
#define BUF_SIZE 256
int main()
{
key_t key;
char *virtualaddr;
sem_t *get, *put;
int shmid;
const char* messageOne = "Hello world , I'm child number 1\n";
const char* messageTwo = "Hello world , I'm child number 2\n";
char buf[BUF_SIZE];
key = ftok("anyfile",'R');
shmid = shmget(key,1024,0644|IPC_CREAT);
...
...
shmctl (shmid, IPC_RMID, NULL);
exit(EXIT_SUCCESS);
}
I get from eclipse undefined reference to sem_open .
I've check with this post since this question is very similar to mine , but didn't understand exactly where is my mistake ,
Can you please explain where do I need to fix it / add another compilation command (if this is indeed the case) ?
Much appreciated
You need to include -lpthread when compiling. This is used by the linker, to link your binary against the library.
The other answers already cover how to do this on the command line.
To do this in Eclipse, you need to follow the directions here:
In the project properties, go to : C/C++ Build --> Settings. Then
"Tool Settings", select "Libraries" under "Linker". You can add all
your project libraries there (without the "-l"). Also in the lower
part, you can add custom path to search libraries
When linking, you have to add the flag -pthread or -lrt to the command line. It's right there in the manual page.
As it is written in the first answer in the question you linked, you nedd to compile it in this way:
gcc source.c -lpthread
-lrt or -pthread will do the same.
I have some code that looks similar to the following:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/ldr.h>
int main (int argc, char **argv)
{
int liRC = 0;
struct shl_descriptor *lstModDesc;
int liEach;
char lsBaseName[513];
char *lsTheName;
for( liEach = 0; liRC == 0; liEach++ )
{
liRC = shl_get( liEach, &lstModDesc );
if( liRC == 0 )
{
strcpy( lsBaseName, lstModDesc->filename );
lsTheName = (char *)basename( lsBaseName );
/* do more stuff */
}
}
return 0;
}
What it is doing is enumerating all the shared libraries attached to the binary. This compiles fine on AIX 5.3 but on AIX 7.1 I am getting the following concerning lstModDesc:
"modulename.c", line 2553.30: 1506-285
(S) The indirection operator cannot be
a pplied to a pointer to an incomplete
struct or union.
I cannot find where shl_get is defined on my aix 5.3 box nor can I find where struct shl_descriptor is defined either. I am stumped. I even tried outputing the preprocessed output with the -E flag to the compiler with no luck. I did a recursive grep in /usr/include. Is there somewhere else I should be searching? Where are those definitions?
Are you sure that bit of the code was included in the compilation on AIX 5.3? I just went Google-whacking with 'site:ibm.com shl_descriptor' and there is precisely one item found:
http://www-01.ibm.com/support/docview.wss?uid=swg21212239
It is pointing to a problem on HP-UX with WAS (WebSphere Application Server). There is sample code which uses <dl.h> (dynamic loader), and shows shl_descriptor and shl_gethandle() and shl_load().
Given the complete absence of hits for anything in AIX and the presence of the HP-UX platform, then you have a slightly different problem to resolve. The question is:
Why is the conditional compilation on AIX 5.3 excluding the section that uses shl_descriptor and not excluding it on AIX 7.1. You should look at the conditions wrapped around that code in the #ifdef line, and see what is used to trigger the HP-only compilation on AIX 5.3.