Link to home
Start Free TrialLog in
Avatar of anthon007r
anthon007r

asked on

STL in class library project (DLL)

Does anyone had tried to include ANY of the following lines in their C++.net class library project:

#include <bitset>
#include <queue>
#include <fstream>
#include <iostream>

im getting LNK2020 errors
(error LNK2020: unresolved token(0A000006) _CxxThrowException)
(error LNK2020: unresolved token(0A000015) delete)

Im using VS .net 2003 with .net 1.1 framework and Winxp with sp2 RC1.

The story behind:
---------------------
Actually, i don't get any error when I included those lines IF im going to create a C++.net windows application. And because FolderBrowseDialog don't seem to work with C++ .net, I wanted to create a DLL so that my GUI is C# or VB.net based and my architecture is lying behind a DLL.. because in that VB.net and C#, FolderBrowseDialog works just fine. I really wonder why. Could anyone answer why? The FolderBrowserDialog doesn't seem to display correctly in C++.net but works just fine in other languages (e.g. it doesn't shows up the treeview).


SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anthon007r
anthon007r

ASKER

Runtime library settings?  What specific settings I should configure?
What I did is I created a class library project under C++ and then under any header file, I tried to include those lines and the errors started to appear.
Note that i didn't do anything yet to the project except I added one of those lines.
I also tried to create a windows form application under C++ then tried to include the header files but there's no any errors. What's wrong? Is STL not allowed in a class library project? (O man, my architecture depends heavily on STL and file stream.. :( )

BTW, thanks drichards for some suggestions about the issue on browseFolderDialog. Is the wrapper a COM based component? CoInitializeEx seems familiar.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great man! That does the job! But actually, there's still a problem.. try this:

1. create a class library project under C++
2. do what you told me (Svetlin Panayotov)
3. create a C++ generic class

#pragma once
#include "string.h"
class sample
{
public:
      sample(void){}
      ~sample(void){}

      void myFunc(char* input)
      {
            input = input;
            input = strrev(input);
      }
};

4. use the new class' function in your main class

and that will lead you to:

1: "error LNK2001: unresolved exeternal symbol "char * __cdecl strrev(char*)"(?strrev@@$$J0YAPADPAD@Z)
2: "fatal error LNK1120: 1 unresolved externals"

Thanks Svetin_Payayotov, your information really helps alot =)

But what about those error? should i include another LIB-file in linker->input?
If you look in string.h where strrev() is defined you'll see something like:

#if     !__STDC__

/* prototypes for oldnames.lib functions */
_CRTIMP void * __cdecl memccpy(void *, const void *, int, size_t);
....
....
_CRTIMP char * __cdecl strrev(char *);
        char * __cdecl strset(char *, int);
_CRTIMP char * __cdecl strupr(char *);

#endif  /* !__STDC__ */


And just few lines above:
_strrev(char *)

Calling _strrev() instead strrev() should work fine.

Svetlin
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
O man, you're really an expert, that does the job! How could you know all of that stuff, which and what to include in linker->input?
Could you please give me some tables? =) please.
Thanks!

ei.. would this warnings be harmful?

msvcrtd.lib(secchk.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(xlock.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(cout.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(iosptrs.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
WArchiver.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(locale0.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
nochkclr.obj : warning LNK4099: PDB 'libc.pdb' was not found with 'C:\Software\SoftDev\Microsoft Visual Studio .NET 2003\Vc7\lib\nochkclr.obj' or at 'C:\Anton\Archiver\WArchiver\Debug\libc.pdb'; linking object as if no debug info

Thanks again Svetlin_Panayotov
now i changed the points value, because im chaining some related questions.

Now I've successfully compiled and built the DLL that contains my architecture with a lot of STL.

But a new problem now arises, if I tried to add my DLL as a reference and tried to call some functions that make use of STL, im getting a very deep error from istream and ios:

"An unhandled exception of type 'System.NullReferrenceException' occured in warchiver.dll

Additional information: Object reference not set to an instance of an object"

To be more detailed, i got those error and the error points to "_Init();"

void init(_Mysb *_Strbuf = 0,
            bool _Isstd = false)
            {      // initialize with stream buffer pointer
            _Init();      // initialize ios_base
            _Mystrbuf = _Strbuf;
            _Tiestr = 0;
            _Fillch = widen(' ');

            if (_Mystrbuf == 0)
                  setstate(badbit);

            if (_Isstd)
                  _Addstd();      // special handling for standard streams
            else
                  _Stdstr = 0;
            }

In my class library DLL project, i only initiatized a fstream variable to isolate the problem, and it seems the declaration of sampleObj below triggers the problem, and when you try to "step-into" during debugging, you'll be pointed in the code i mentioned above.

#include "fstream"
using namespace std;
using namespace System;

namespace myNamespace
{
   public __gc class  myClass()
   {
        public:
           void myFunc()
           {
                fstream sampleObj;
           }
   }

}

Please help. Thanks.

i forgot, I got those errors when I tried to call a function in my DLL class library in my C# Windows application project.
The function I called from my DLL class library instantiated a fstream class which triggers the error.
i forgot to mention.. the line that generates the error in the 9th comment above is in IOS header.
Haven't seen soo many mails later :)
Now - one by one - I don't have a table...you just have to know this stuff...it comes with the time :)
The warnings should not be harmful.
About your last problem - I don't know what went wrong.I even created test library in managed c++ and c# app that calls it - but nothing wrong with fstream.The constructor works fine - writing to file too.
Maybe you need to take a closer look at your code (or do the same - just create test lib and app).
Or...maybe you need some sleep? ;)

Svetlin
yup i did create a sample test lib.. and it didn't worked.. maybe you could post here what you did?
Here are things what i did:

1. I created a class library project named SampleCL
2. I edited the SampleCL.h to:

#pragma once

#include "fstream" // added by me

using namespace std; // added by me
using namespace System;

namespace SampleCL
{
      public __gc class Class1
      {
      public: // added by me
            Class1() // added by me
            {
                  fstream fileIO;
            }
      };
}

3. in all configuration in linker->input i added MSVCRTD.LIB
4. built the DLL and that produced the SampleCL.dll
5. Create a new Windows Application in C#.net
6. I add SampleCL.dll as a reference
7. I dragged a button
8. Double clicked it and edited the eventhanlder to this:

            private void button1_Click(object sender, System.EventArgs e)
            {
                  SampleCL.Class1 obj = new SampleCL.Class1();
            }

9. Running the winapp and clicking the button1 will produce the error..

i dunno why.. its just a simple application.. im getting desperate.. please help
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This IS the question i asked im most satisfied with the answers! May I know your secrets? LOL im really impressed =)
Well...thanks - I'm happy to help :)
And about the secrets - you need - PC, good books and a lot of cigarettes & coffee ;)
Happy coding

Svetlin