Showing results for 
Search instead for 
Did you mean: 

Compile VL6180X API in c

Associate III

I'm trying to get a VL6180X sensor to work.
I've downloaded the latest API:
My goal is to make the sensor work in I2c on a Raspberry PI in the c or c++ language.
But I can't compile my code, or rather compile the api into a library.
Where can I find an example of a makefile or command to do this?

Thanks for your help.

Andrew Neil
Evangelist III

Did you mean to ask a question?


EDIT:  OP has now been updated to include a question


Andrew Neil
Evangelist III

@SCADE wrote:

But I can't compile my code, or rather compile the api into a library.

What problem(s), exactly, are you facing?

What have you tried so far?

Again, please see:


Ok so here is my test.cpp file :

#include <iostream>
#include <unistd.h>
#include "vl6180_api.h"

int main()
  VL6180Dev_t tof = 0x29;


  for (size_t i = 0; i < 10; i++)

    VL6180_RangeData_t rangeData;
    VL6180_RangePollMeasurement(tof, &rangeData);

    printf("Range: %d\n", rangeData.range_mm);

  VL6180_FilterSetState(tof, 1);

  return 0;

file structure : 

and finally compile command :
g++ -o test test.cpp api_vl6180x_1.4.0/core/src/*.c api_vl6180x_1.4.0/platform/cci-i2c/*.c -I api_vl6180x_1.4.0/core/inc/ -I api_vl6180x_1.4.0/platform/template/ -I api_vl6180x_1.4.0/config/ -lwiringPi

Error :
/usr/bin/ld: /tmp/ccnVF5wY.o: in function `VL6180_WaitDeviceBooted':
vl6180_api.c:(.text+0x6c): undefined reference to `VL6180_RdByte' ...................................... 

@SCADE wrote:

Error :
/usr/bin/ld: /tmp/ccnVF5wY.o: in function `VL6180_WaitDeviceBooted':
vl6180_api.c:(.text+0x6c): undefined reference to `VL6180_RdByte' ...................................... 

"undefined reference" is a linker error.

It means that you have omitted to provide a definition of that function - either as source code, or as a pre-built binary library.

Probably, you have #included a header which just declares that function (ie, just provides a prototype) - hence the compiler is happy - but haven't added the necessary source or pre-built binary library to your project - so the link fails.





Looks like these are the docs you need:



This is also what I understand the definition seems to be missing, however it is present in

for VL6180_RdByte :
Function declaration: vl6180_api.h
Function definition: vl6180_i2c.c
Function usage: vl6180_api.c

And I seem to be integrating them all correctly in my compile command

When we write the code you are using, we don't know what MCU ( or CPU) you have.

So, we write the code all the way to RdByte() and WrByte() and then WE STOP.

It's up to you to populate those functions.

Have a look at the platform.c function and Platform.h

Notice they are basically empty?

There are examples of VL6180 projects on Linux - on GitHub.

Download one of those and borrow the Platform.c file. 

You basically have to insert a IOCTL I2C write function into RdByte(). 

- john

Our community relies on fruitful exchanges and good quality content. You can thank and reward helpful and positive contributions by marking them as 'Accept as Solution'. When marking a solution, make sure it answers your original question or issue that you raised.

ST Employees that act as moderators have the right to accept the solution, judging by their expertise. This helps other community members identify useful discussions and refrain from raising the same question. If you notice any false behavior or abuse of the action, do not hesitate to 'Report Inappropriate Content'

Thanks for your reply,

The API documentation recommends using cci-i2c (should work on raspberry??)
In the files of the cci-i2c folder there are the functions you say are missing but that I can't get to work ;(

And on Github I couldn't find anything relevant.