3

I have created one static "C" library using VS.

I am using the same library file for another VS console C application its working fine but when I am working with windows forms app it's not working.

Referred so many queries in this forum but didn't get the Help.

Is there any naming conventions to call the static library functions from the Windows forms Managed c++ ?

Getting Errors Like this

error LNK2028: unresolved token (0A000032) "enum STATUS __clrcall xyz(unsigned char)" (?xyz@@$$FYM?AW4STATUS@@E@Z) referenced in function __catch$?button3_Click@Form1@Myapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z$0

But I should use the same static library for both console and windows application.

6
  • which version of VS are you using? Commented Feb 19, 2014 at 7:09
  • You don't "import" them, you link them. Surely a hint that you are doing it wrong, but "its not working" gives very little to go by. If you see the name of a function in the library in a linker error message and it also says "__clrcall" then you forgot to tell the compiler that the libraries contain unmanaged code. Of course you should have posted the errors that you get. They probably read like Greek to you but tell us what is going wrong. Commented Feb 19, 2014 at 8:30
  • Getting Errors Like this error LNK2028: unresolved token (0A000032) "enum STATUS __clrcall xyz(unsigned char)" (?xyz@@$$FYM?AW4STATUS@@E@Z) referenced in function __catch$?button3_Click@Form1@Myapp@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z$0 But i should use the same static library for both console and windows application. Commented Feb 19, 2014 at 8:37
  • yes libraries have native code @HansPassant Commented Feb 19, 2014 at 8:38
  • Has the routine been declared as extern "C"? Commented Feb 19, 2014 at 8:49

1 Answer 1

5

The linker error message gives a strong hint what it going wrong. Note the __clrcall calling convention for the undefined symbol, it tells you that the compiler thinks that these are "CLR" functions. Managed code, of course they are not, they are __cdecl. There's more, the names are also mangled. Note the "@@$$FYM?AW4STATUS@@E@Z" curses in the name. Which tells you that the compiler thinks they were written in C++ instead of C.

You'll have to explicitly tell the compiler about this, the .h file isn't compatible enough. Which you do like this in your C++/CLI source code file:

#pragma managed(push, off) extern "C" { #include "yadayada.h" } #pragma managed(pop) 

The #pragmas temporarily turn off managed code compilation mode so the compiler will now assume these are unmanaged function declarations. The extern "C" {} wrap around the #include tells the compiler that the .h file contains C declarations.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer but it is showing fatal error LNK1313: ijw/native module detected; cannot link with pure modules
That's another mistake, don't use /clr:pure when you link in native code. That's not pure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.