Problems using reference include Headers on C++ project.
hauk at tildeslash.com
Thu Oct 27 03:10:05 CEST 2011
On Oct 26, 2011, at 5:56 PM, Antonio Castellon - RAC wrote:
Interesting. This has nothing to do with extern "C" which I mentioned earlier, though extern "C" should be added to libzdb headers if to be used from C++.
Out of curiosity I tested this and got the same error:
> /usr/local/include/zdb/URL.h:55: error: conflicting declaration ‘typedef struct URL_T* URL_T’
> /usr/local/include/zdb/URL.h:55: error: ‘struct URL_T’ has a previous declaration as ‘struct URL_T’
It seems that the problem has to do with forward declaration and namespaces in C vs C++. In C, structs are in a separate namespace so it is perfectly legal to forward declare and typedef a struct to a type of the same name. That is;
typedef struct URL_T *URL_T;
But this does not work in C++ (as far as I understand, I'm no C++ guy though). In C++ a struct is (basically) the same as a class, so typedef *URL_T is in the same namespace as struct URL_T, hence the curious compiler error "‘struct URL_T’ has a previous declaration as ‘struct URL_T’". The "workaround" is simple; just give the two types different names. For instance lowercase the struct name:
typedef struct url_t *URL_T;
Since we use this typedef forward declaration idiom to declare all API objects in libzdb you would have to modify all the 5-6 API header files and corresponding source code where the type is declared, if you still want to use libzdb from C++. It is a question of modifying only 5 lines or so. Though I'm not inclined to do the change in the repository since this just demonstrated how C is superior to C++ (Obviously I'm a C guy :)
More information about the libzdb-general