Compiling header using C, instead of C++ -
i have c++ project have configured using cmake use eclipse. problem have added static c library (namely, svm-struct/svm-light) doesn't seem compile- , guess it's compiling c++ instead of c.
i added library project follows:
set(svm_light_src_dir "../../libraries/svm_rank") include_directories(${svm_light_src_dir}) add_library( svm_rank_lib static ${svm_light_src_dir}/svm_light/svm_learn.c ${svm_light_src_dir}/svm_light/svm_common.c ${svm_light_src_dir}/svm_light/svm_hideo.c ${svm_light_src_dir}/svm_struct/svm_struct_learn.c ${svm_light_src_dir}/svm_struct/svm_struct_common.c ${svm_light_src_dir}/svm_struct/svm_struct_classify.c ${svm_light_src_dir}/svm_struct_api.c ${svm_light_src_dir}/svm_struct_learn_custom.c ) add_executable(${project_name} ${sources}) target_link_libraries(${project_name} svm_rank_lib)
cmake configures fine seems. within output of configuration specifies finds c , c++ compilers , "work". add header 1 of project files using extern follows:
#ifdef __cplusplus extern "c" { # include "svm_struct/svm_struct_common.h" } #endif
when go build project error here:
../../libraries/svm_rank/svm_struct/../svm_struct_api_types.h:75:11: error: expected member name or ';' after declaration specifiers double *class; /* vector of scores imply ranking */ ~~~~~~ ^ 1 error generated.
there variable within library header called "class" error occurs, , guess it's trying compile library header using c++ instead of c. first of reason error? if so, how should go fixing this?
as has been pointed out, source of problem c library header declares variable named class
, keyword in c++.
this problem hit header pulled in c++ source file. remember headers not compiled themselves, merely copy-pasted preprocessor source file #include
s them. type of source file determines whether code in header interpreted c or c++.
the fact wrapped include in extern "c"
not change this. switches off c++-style name mangling declarations in header, code still has compile valid c++.
the cleanest solution problem technique known insulation or compiler firewall.
you have make sure parts come contact problematic library c-source files themselves. c++ part of code interacts library through interface of c part, never library directly. in particular, must never #include
library headers of header files.
for instance: my_interface.c
#include "svm_struct/svm_struct_common.h" /* safe include .c file */ struct opaque_ { /* can use types svm_struct_common in here */ }; opaque* initialize() { /* can create opaque_ on heap , manipulate here, give pointer c++ part */ } void do_stuff(opaque*) { /* whatever stuff in opaque */ }
my_interface.h
/* no #includes in header! */ /* opaque type forward declared! c++ code can obtain pointer it, cannot inside */ struct opaque_; typedef struct opaque_ opaque; opaque* initialize(); void do_stuff(opaque*);
my_application.cpp
// include our own header, made sure valid c++ extern "c" { #include <my_interface.h> } void do_stuff() { opaque* context = initialize(); do_stuff(context); }
Comments
Post a Comment