xcode c/c++ linker error: undefined symbol -


i did search problem seems trivial no 1 asked.
have mixed c , c++ code.

in model.h, have following declaration:

void free_and_null (char **ptr); 

in model.cpp, have function body:

void free_and_null (char **ptr) {     if ( *ptr != null ) {         free (*ptr);         *ptr = null;     } } /* end free_and_null */ 

in solve.c file, have following :

#include "sort.h" .....     free_and_null ((char **) &x); 

when compile project, has following linker error:

wrap) undefined symbols architecture x86_64: "_free_and_null", referenced from:

why such simple program can have error? use apple llvm 6.0 compiler. input highly appreciated.

you need tell c++ compiler function should have "c" linkage declaring such (in c++ source file).

extern "c" {   void free_and_null (char **ptr); } 

c++ compilers construct "mangled" names functions 2 functions same name different argument types end having different mangled names; that's necessary because linkers expect names unique. consequently, code generated c++ compiler in example produce different name name produced c compiler. (on intel platforms, c compilers modify names slightly, prefixing underscore, why complains _free_and_null unfound.)

one common way of doing make header file uses preprocessor macros detect language. it's not portable, should work in case:

// file model.h // not work c++ compilers, works clang , gcc #ifdef __cplusplus extern "c" { #endif  void free_and_null (char **ptr); // other function declarations here  #ifdef __cplusplus } #endif 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -