Tuesday 15 February 2011

C out of header in C++ -


If I use C code in C ++ environment and I include all the codes in the header, then Everything works fine if I try to declare the C function in the header and any of them C or. CPP file, I receive the following error:

  Undefined symbols for architecture x86_64: "vec2_norm (Vec2)", referenced from: _main: ld: symbol in the main menu (S) not found for architecture x86_64 clags: error: failed with linker command failure code 1 (Use -V to see invitations)   

VK2H

  #ifndef physics_in_in_stays_wiki2_h #defined physics_ign_stack _vike 2_ac Type Fef Strait {float x; Float Y; } VAC2; Inline Vec2 vec2_norm (VAC2V); #endif   

Vec2.c or .cpp

  #include "Vec2.h" # Include & lt; Math.h> Inline Vec2 vec2_norm (Vec2 v) {float lane = vx; v.x + v.y * v.y; If (lane) {lenan = 1 / sqrtf (lane); V.x * = len; V.y * = len; } Return V; Although I have tested your code sample, and it has been compiled exactly for me using G ++ /.  
extern "c" {} around, so that the compiler does not mangling C ++ names on those functions:
  #ifdef __cplusplus extern "C "{#endif typedef struct {float x; Float Y; } VAC2; Inline Vec2 vec2_norm (VAC2V); #ifdef __cplusplus}; #endif   

and

  extern "C" {inline Vec2 vec2_norm (Vec2 v) {float len ​​= v.x * v.x + v.y * v.y; If (lane) {lenan = 1 / sqrtf (lane); V.x * = len; V.y * = len; } Return V; }}; Regarding   

Btw, inline being used in your code, even if it mandatory not to define inline function In the header only, it is strongly recommended to do this, so you do not need to copy the inline body into each translation unit where you have to include the header due to a definition rule You can. Like Pipes: / p>

Some things, such as types, templates and extension inline functions, can be defined in more than one translation unit. For a given institution, each definition should be the same. There are different organizations in different non-external objects and functions in different translation units, even if their names and types are the same.

But in the end, is it a good idea to have a good idea about your design options?

HH

No comments:

Post a Comment