If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to use custom "C" library with C++ codes

I came across a very strength situation while experimenting with creating custom libraries. In my custom library header, I just have this line of code: "double BankAccountBal ( ) ;". my implementation for the library is : "double BankAccountBal ( ) {return 1500.25;};" I then tried compiling this library using C code via setting the Compiled As under Property->C/C++ ->Advanced-> Compiled As to : "Compiled as C Code (/TC). The library compiled fine. Then after the library is built, I include the library in my main project (and properly include all the correct paths, the library link, etc., ), and in my main () I have this line of code: std::cout << BankAccountBal() << std::endl; I tried to build, and got this error : error LNK2019: unresolved external symbol "double __cdecl BankAccountBal(void)" (?BankAccountBal@@YANXZ) referenced in function _wmain. Again, I assured you I have set all the correct paths and link variables. But .... if I went back to the library and compiled as C++ codes instead, via setting the Compiled As under Property->C/C++ ->Advanced-> Compiled As to : "Compiled as C++ Code (/TC). Everything worked !

This suggests that if we are to build C libraries to be used by C++ main codes, there are something missing that need to be set, but I don't know what it is. Any help will be greatly appreciated. Thanks.

Re: How to use custom "C" library with C++ codes

Originally Posted by zz1234

In my custom library header, I just have this line of code: "double BankAccountBal ( ) ;".

This is where you're wrong. C and C++ are different languages. So, the same line is interpreted different been compiled in C unit compared to C++ one. Compiled in C the line declares _BankAccountBal symbol while in C++ it is ?BankAccountBal@@YANXZ. So you need a special trick to make this be compiled identically: