0

I have a problem with declaring and calling a function from a shared library with arguments from a different namespace in C++. I am working with two different shared libraries (lib1.a and lib2.a) that use C++ (pointers to) structs as input for their functions in the same Code::Blocks 20.03 project (on Linux). The C++ structs for lib1 and lib2 have the same names (e.g. struct_a) but not the same contents. Since I have no access to the functions inside the shared libraries, I decided to define the structs in different namespaces (space1(=std) and space2). I declare and call the functions in the following way (only summarized code, way too complicated and confusing):

using namespace std;
extern LINKAGE short func_lib2(space2::struct_a);
space2::struct_a mystruct2;

int main(){
space2::struct_a_pointer mypointer2 = &mystruct2;     
myshort = func_lib2(mypointer2);
return 0;
}

(the structs and pointers are defined elsewhere for the std namespace and space2 namespace)

On Windows Visual Studio 2022 this principle works fine. On Linux Code::Blocks I get a build error "undefined reference to 'func_lib2(space2::struct_a*)'" in the line where I call the function. I have tried:

  • Declaring and calling the function with std namespace struct_a -> no build error (but will give errors when executing the code because struct has wrong entries), so no problem with linking the library
  • Delete call of function inside the main() -> no build error, problem lies within the call or in some disagreement between declaration and call
  • Functions that I built myself using structs of both namespaces e.g. void convert_structs(struct_a_pointer mypointer1, space2::struct_a_pointer mypointer2){...} -> no build error, namespaces seem to be well defined
  • Declaring and calling the whole function in space2 e.g. namespace space2 { extern LINKAGE short func_lib2(struct_a); } -> error 'func_lib2 is not defined in this scope'
  • Setting the same C and C++ language standards on both Windows and Linux compilers -> no change

Now my questions: Is it in general possible to declare and call functions like this? Should there be no difference between Linux and Windows? Or am I disobeying some fundamental rules for C++ namespaces? Is this a problem of the shared libraries (they are obviously different for Linux and Windows, lib1.a and lib1.dll) that needs fixing by the library provider?

Thanks a lot!

katmir
  • 1
  • 1

0 Answers0