Mixing C and C++
While writing ROS code, I got stuck and spent 3 hours on solving these problems. Mixing C and C++ might be inevitable if you use ROS, however, I'm not sure how easy this should have been for non-beginner programmers...
Prob-1. how to call third party .h files written in C from .cpp files. Here is the answer.
link
Prob-2. Compiling c file using g++ causes error that didn't occur when I use gcc. The error looks like this and I haven't solved yet.
Prob-1. how to call third party .h files written in C from .cpp files. Here is the answer.
link
[32.3] How can I include a non-system C header file in my C++ code?
If you are including a C header file that isn't provided by the system, you may need to wrap the #include line in an extern "C" { /*...*/ } construct. This tells the C++ compiler that the functions declared in the header file are C functions.
// This is C++ code
extern "C" {
// Get declaration for f(int i, char c, float x)
#include "my-C-code.h"
}
Prob-2. Compiling c file using g++ causes error that didn't occur when I use gcc. The error looks like this and I haven't solved yet.
[robot@localhost heracleia]$ make
g++ -o wheel controlByInput.c steeringWheel.cpp iravoid.o bump.o setSpeedVar.o -L/home/robot/robots/lib -lpthread -lrobot -I/home/robot/robots/include
controlByInput.c: In function ‘int lcdCont()’:
controlByInput.c:39: error: invalid conversion from ‘unsigned char*’ to ‘char*’
controlByInput.c:39: error: initializing argument 1 of ‘int sprintf(char*, const char*, ...)’
controlByInput.c: In function ‘int main(int, char**)’:
controlByInput.c:96: error: invalid conversion from ‘void*’ to ‘pthread_t*’
controlByInput.c:104: error: invalid conversion from ‘void*’ to ‘pthread_t*’
controlByInput.c:112: error: invalid conversion from ‘void*’ to ‘pthread_t*’
controlByInput.c:114: error: invalid conversion from ‘void* (*)(VAR*)’ to ‘void* (*)(void*)’
controlByInput.c:114: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
controlByInput.c:148: error: ‘getButtons’ was not declared in this scope
make: *** [wheel] Error 1
Comments