tsnorthern
Posts: 5
Joined: Sat May 05, 2018 8:54 pm

Can't Install/Load Accelerometer C Library

Wed May 09, 2018 2:44 am

Howdy Y'all,

I'm trying to load the following library:

https://github.com/Ybalrid/mma8451_pi

and i've attempted to use the Makefile to install the library, but get this error:

Code: Select all

c -Iinclude -DVERSION=\"0.0.1\" -fPIC -g   -c -o src/mma8451_pi.o src/mma8451_pi.c
src/mma8451_pi.c: In function ‘mma8451_write_byte’:
src/mma8451_pi.c:16:20: error: array type has incomplete element type
     struct i2c_msg messages[1]; //We are just writing one byte to the device
                    ^
src/mma8451_pi.c: In function ‘mma8451_read_byte’:
src/mma8451_pi.c:45:20: error: array type has incomplete element type
     struct i2c_msg messages[2];
                    ^
src/mma8451_pi.c:56:25: error: ‘I2C_M_RD’ undeclared (first use in this function)
     messages[1].flags = I2C_M_RD;
                         ^
src/mma8451_pi.c:56:25: note: each undeclared identifier is reported only once for each function it appears in
src/mma8451_pi.c: In function ‘mma8451_read_multibyte’:
src/mma8451_pi.c:78:20: error: array type has incomplete element type
     struct i2c_msg messages[2];
                    ^
src/mma8451_pi.c:91:25: error: ‘I2C_M_RD’ undeclared (first use in this function)
     messages[1].flags = I2C_M_RD;
                         ^
<builtin>: recipe for target 'src/mma8451_pi.o' failed
make: *** [src/mma8451_pi.o] Error 1
I also tried to pull the .h and .c files for the library and place them directly into the folder of the code i'm looking to run (which is just the test from the library). It managed to compile but wouldn't build into an exe. I get this error:

Code: Select all

gcc -Wall -o "main" "main.c" (in directory: /home/pi/Desktop/mma8451_pi/include)
/tmp/ccAI7g34.o: In function `main':
main.c:(.text+0x24): undefined reference to `mma8451_initialise'
main.c:(.text+0x44): undefined reference to `mma8451_get_acceleration'
Compilation failed.
collect2: error: ld returned 1 exit status
I'm totally stuck here. I've used all the underhanded C tricks i know (which is very few), but i'm stuck trying to get this thing to run.

Anyone have any good (or even bad) ideas? Maybe some sage wisdom? Am i doing something dumb here? Thanks in advance y'all.

User avatar
topguy
Posts: 4736
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Can't Install/Load Accelerometer C Library

Wed May 09, 2018 1:56 pm

I'm guessing the i2c types and definitions are included from "#include <linux/i2c-dev.h>" so make sure that you have that file.

Code: Select all

find /usr -name i2c-dev.h

tsnorthern
Posts: 5
Joined: Sat May 05, 2018 8:54 pm

Re: Can't Install/Load Accelerometer C Library

Sat May 12, 2018 2:18 am

I've done as discussed above, found that i do in fact have that file, and included the #include for it as listed above in the C Code. Still getting the same issue. Maybe i don't fully grasp what's going on during compilation/make? The compilation works fine, but the Make tanks. Is there more to this than navigating to the directory and running 'Sudo Make'? Does something need to be added to the command?

User avatar
Paeryn
Posts: 2103
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Can't Install/Load Accelerometer C Library

Sat May 12, 2018 1:10 pm

The source file src/mma8451_p1.c needs updating, i2c-dev.h doesn't include i2c.h itself. Change the start of that file to

Code: Select all

#include "mma8451_pi.h"
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
She who travels light — forgot something.

tsnorthern
Posts: 5
Joined: Sat May 05, 2018 8:54 pm

Re: Can't Install/Load Accelerometer C Library

Thu May 17, 2018 2:00 am

Ok, i've fixed the source file and now the makefile runs, but i still can't seem to get the C code to build after it successfully compiles. I get the following error:

Code: Select all

gcc -Wall -o "main" "main.c" (in directory: /home/pi/Desktop/mma8451_pi/include)
/tmp/ccgLPlvm.o: In function `main':
main.c:(.text+0x24): undefined reference to `mma8451_initialise'
main.c:(.text+0x44): undefined reference to `mma8451_get_acceleration'
collect2: error: ld returned 1 exit status
Compilation failed.
Now clearly there's some issue with the code finding the definitions of these functions, but i'm confused as to how this could be happening given that they are all in one directory. Maybe i need to directly include the library in the build command? I can't seem to find a syntax with the '-l' modifier that actually runs. Any ideas?

Also, thanks for the help y'all. I was up a creek without a paddle on this.

User avatar
Paeryn
Posts: 2103
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Can't Install/Load Accelerometer C Library

Thu May 17, 2018 2:01 pm

tsnorthern wrote:
Thu May 17, 2018 2:00 am
Ok, i've fixed the source file and now the makefile runs, but i still can't seem to get the C code to build after it successfully compiles. I get the following error:

Code: Select all

gcc -Wall -o "main" "main.c" (in directory: /home/pi/Desktop/mma8451_pi/include)
/tmp/ccgLPlvm.o: In function `main':
main.c:(.text+0x24): undefined reference to `mma8451_initialise'
main.c:(.text+0x44): undefined reference to `mma8451_get_acceleration'
collect2: error: ld returned 1 exit status
Compilation failed.
Now clearly there's some issue with the code finding the definitions of these functions, but i'm confused as to how this could be happening given that they are all in one directory. Maybe i need to directly include the library in the build command? I can't seem to find a syntax with the '-l' modifier that actually runs. Any ideas?

Also, thanks for the help y'all. I was up a creek without a paddle on this.
Your compile line doesn't tell the linker to link the file containing the library's code which is why the linker is complaining about not being able to find those functions, it doesn't know where to find them. You need to tell it the name of the library (in this case mma8451_pi) by adding

Code: Select all

-lmma8451_pi
to the compile line possibly with

Code: Select all

-Lpath-to-library
to tell the linker what directory to look in to find the library if it isn't in one of the standard library search paths. If the headers aren't in the normal header search path either then you need to add the location of them to the compile line too with

Code: Select all

-Ipath-to-include-files
According to what you say, the file you are compiling (main.c) is in the library's include directory, that is a very strange place to put your code. Really you shouldn't be putting your own files in the source tree of another library lest you mess something up.

If you have compiled the library with make and then installed it with sudo make install then your compile line to use it should be

Code: Select all

gcc -Wall -o main main.c -lmma8451_pi
no matter what directory you have your source code in as the install puts the headers and library files into the fairly standard locations of /usr/local/include and /usr/local/lib
She who travels light — forgot something.

tsnorthern
Posts: 5
Joined: Sat May 05, 2018 8:54 pm

Re: Can't Install/Load Accelerometer C Library

Sun May 20, 2018 7:41 pm

Ok, i managed to resolve the issues above (when i ran the make file, i didn't run

Code: Select all

sudo make install
, just

Code: Select all

sudo make
) and now i'm so close to getting this to run properly i can almost taste it.

Here's where i'm at now:

1. The file compiles
2. The file builds into an exe properly
3. The exe runs, but produces no output
4. When i run the exe in the command prompt, i get the following error:

Code: Select all

./Accel_Data: error while loading shared libraries: libmma8451_pi.so: cannot open shared object file: No such file or directory
More interesting, i build a test file just to see if the exe's i'm making can actually write to the command prompt. It contains the following code:

Code: Select all

#include <stdio.h>

int main(int argc, char **argv)
{
	printf("Testing testing testing");
	return 0;
}
It actually produces the same error, despite not using the library in question:

Code: Select all

./TestCode: error while loading shared libraries: libmma8451_pi.so: cannot open shared object file: No such file or directory
Any ideas what causes this? There seems to be a missing file, but the library installed properly per the Make file. I'm not sure what that file i'm missing actually does, or how to fix this issue. Any ideas guys? Big thanks for the help so far; i'm awful close to getting this run.

User avatar
topguy
Posts: 4736
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Can't Install/Load Accelerometer C Library

Mon May 21, 2018 8:10 pm

You can try a "sudo ldconfig" which should refresh the list of available libraries.

Also you can try a "find /usr/local/ -name "libmma8451_pi*" to verify that the library has been installed.

tsnorthern
Posts: 5
Joined: Sat May 05, 2018 8:54 pm

Re: Can't Install/Load Accelerometer C Library

Wed May 23, 2018 2:20 am

Wow. The

Code: Select all

Sudo ldconfig
totally solved the problem. It now runs and produces outputs (incorrect outputs, but still outputs). Thanks for all the help y'all, i honestly wasn't sure i was ever going to solve this. Thanks!

Return to “Troubleshooting”

Who is online

Users browsing this forum: Bing [Bot], HawaiianPi and 47 guests