Answer the question
In order to leave comments, you need to log in
Linking a C library to a C++ application. How?
There is a C library built like this:
gcc -fPIC -c DHT_Driver/common_dht_read.c -o DHT_Driver/common_dht_read.o -std=gnu99
gcc -fPIC -c DHT_Driver/pi_dht_read.c -o DHT_Driver/pi_dht_read.o -std=gnu99
gcc -fPIC -c DHT_Driver/pi_mmio.c -o DHT_Driver/pi_mmio.o -std=gnu99
gcc -shared DHT_Driver/common_dht_read.o DHT_Driver/pi_dht_read.o DHT_Driver/pi_mmio.o -o DHT_Driver/libdht_driver.so
extern int pi_dht_read(int type, int pin, float* temperature, float* humidity);
g++ main.cpp -ldht_driver
/tmp/ccxRKglu.o: In function `main':
main.cpp:(.text+0x1c): undefined reference to `pi_dht_read(int, int, float*, float*)'
collect2: error : ld returned 1 exit status
Answer the question
In order to leave comments, you need to log in
Try
extern "C" int pi_dht_read(int type, int pin, float* temperature, float* humidity);
Everything as suggested by @Samuel_Leonardo , but most likely you are feeding this code to the C compiler. The C compiler does not know about extern "C". C++ knows about it. Should be something like:
#ifdef cplusplus
extern "C"
#endif
int pi_dht_read(int type, int pin, float* temperature, float* humidity);
#ifdef cplusplus
extern "C" {
#endif
int pi_dht_read(int type, int pin, float* temperature, float* humidity);
#ifdef cplusplus
}
#endif
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question