473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Exporting C++ Classes from a Static Library and from a DLL

I am not sure if this OT, but I will go ahead and post. If it has to be
posted elsewhere let me know.
-vijai.
------

I have a class declared as follows:

#pragma once

// a simple reference counter implementation

#ifndef __REF_COUNTER_H__
#define __REF_COUNTER_H__

#include <iosfwd>
#include "smlibdefs.h"

namespace SMLib
{

class SMLIBAPI_CLASS ReferenceCounter
{
unsigned long m_numrefs;
public:
ReferenceCounter();
ReferenceCounter(const ReferenceCounter&);
ReferenceCounter& operator = (const ReferenceCounter&);
virtual ~ReferenceCounter();

virtual void AcquireReference();
virtual void ReleaseReference();
virtual unsigned long CountReferences() const;
};

};

SMLIBAPI_FUNCTION std::ostream& operator << (std::ostream&, const
SMLib::ReferenceCounter&);
SMLIBAPI_FUNCTION std::wostream& operator << (std::wostream&, const
SMLib::ReferenceCounter&);

#endif

SMLIBAPI_FUNCTION is defined as __declspec(dllimport) or
__declspec(dllexport) depending on whether the class is being imported
or exported. (On Windows. On *nix I don't have to do anything to export
C++ classes I believe. All symbols are exported by default?)

I build a shared library and a static library. I test the class and
it's methods with the following separate test program.

#include <refcounter.h>
#include <windows.h>
#include <iostream>
#include <assert.h>

void Test_ReferenceCounter_Construction()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter: "<<ctr<<std::endl;
}

void Test_ReferenceCounter_Initialization()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter: "<<ctr<<std::endl;
assert(ctr.CountReferences() == 0);
}

void Test_ReferenceCounter_CopyConstruction()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter 1: "<<ctr<<std::endl;
SMLib::ReferenceCounter ctr2 = ctr;
std::wcout<<L"Counter 2: "<<ctr2<<std::endl;
assert(ctr2.CountReferences() == 0);
}

void Test_ReferenceCounter_Assignment()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter 1: "<<ctr<<std::endl;
ctr.AcquireReference();
ctr.AcquireReference();
SMLib::ReferenceCounter ctr2;
ctr2.AcquireReference();
std::wcout<<L"Counter 2: "<<ctr2<<std::endl;
ctr2 = ctr;
std::wcout<<L"Counter 2 (reassigned): "<<ctr2<<std::endl;
assert(ctr2.CountReferences() == ctr.CountReferences());
}

void Test_ReferenceCounter_ReferenceIncrement()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter: "<<ctr<<std::endl;
ctr.AcquireReference();
assert(ctr.CountReferences() == 1);
}

void Test_ReferenceCounter_ReferenceDecrement()
{
SMLib::ReferenceCounter ctr;
std::wcout<<L"Counter: "<<ctr<<std::endl;
ctr.AcquireReference();
ctr.AcquireReference();
ctr.ReleaseReference();
assert(ctr.CountReferences() == 1);
}
int main()
{
LoadLibrary("smlib-1.0.0.0");

Test_ReferenceCounter_Construction();
Test_ReferenceCounter_Initialization();
Test_ReferenceCounter_Assignment();
Test_ReferenceCounter_ReferenceIncrement();
Test_ReferenceCounter_ReferenceDecrement();

return 0;
}

However, in the shared library:

I get an unresolved external error only for the overloaded left shift
operator. On the other hand for the static library I get unresolved
external symbols error for all the methods as well as the function. I
use the same makefile to compile both:
executableName = smlib
programName = smlib

version = 1.0.0.0

baseDir = .
incDir = $(baseDir)/
srcDir = $(baseDir)/

!if defined(DEBUG) || defined(_DEBUG)
buildSuffix = d
buildType = DEBUG
!else
buildSuffix =
buildType = RELEASE
!endif

ExtLibs = kernel32.lib \
msvcrt$(buildSuffix).lib \
libc$(buildSuffix).lib \
libcmt$(buildSuffix).lib

Includes = basedefs.h \
smlibdefs.h \
refcounter.h

OutPath = $(MAKEDIR)\$(buildType)

CCOpts = /c /Gd /MDd /EHa /D__SMLIB__BUILD /D$(buildType) /I$(incDir)
CC = cl.exe

LibOpts = /OUT:$(OutPath)\libsmlib-$(version).lib /NOLOGO
/SUBSYSTEM:NATIVE
LIB = lib.exe

LinkOpts = /$(buildType) /DLL /NOLOGO
/OUT:$(OutPath)\smlib-$(version).dll
/IMPLIB:$(OutPath)\smlib-$(version).lib
/PDB:$(OutPath)\smlib-$(version).pdb $(ExtLibs)
LINK = link.exe

refcounter: refcounter.cpp $(Includes)
$(CC) $(CCOpts) /Fo$(OutPath)/refcounter.obj refcounter.cpp

allObjs = $(OutPath)\refcounter.obj
lib: refcounter
$(LIB) $(LibOpts) $(allObjs)

dll: refcounter
$(LINK) $(LinkOpts) $(allObjs)

all: refcounter dll lib

clean:
del /Q $(OutPath)\*.obj
del /Q $(OutPath)\smlib-$(version).pdb
del /Q $(OutPath)\smlib-$(version).lib
del /Q $(OutPath)\smlib-$(version).exp
del /Q $(OutPath)\smlib-$(version).dll
del /Q $(OutPath)\smlib-$(version).ilk

Anyone have any ideas on what I am doing wrong?

thanks,

-vijai.

Aug 20 '05 #1
1 6223
Vijai Kalyan wrote:
I am not sure if this OT, but I will go ahead and post. If it has to
be posted elsewhere let me know.
Letting you know: microsoft.public.vc.language.
[...]
Anyone have any ideas on what I am doing wrong?


Somebody in the VC++ newsgroup will tell you, no doubt. Your code
is so full of non-standard stuff that I don't think it is worth
picking apart here, in comp.lang.c++.

V
Aug 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Outshined | last post by:
I have a class library where just about every class uses its static initializer to register with a central registry object in the same assembly. I am hoping for some sort of Assembly.Load event...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
4
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
7
by: Gustavo L. Fabro | last post by:
Greetings! Some classes that once compiled without problems on VS 2003 have now problems on VS 2005 Beta 1. I'm talking about a __nogc class that is exported with __declspec(dllexport). The...
6
by: Notre Poubelle | last post by:
Hello, I have a strange situation where I'm trying to export a function in a class, but it won't get exported. I actually have a couple of header and cpp files. Each pair of header and cpp...
12
by: 2b|!2b==? | last post by:
I want to export my C++ classes in a DLL, using ordinal # - rather than by name. Will anyone care to enumerate through the steps required to do this? I am already failiar with exporting classes...
1
TRScheel
by: TRScheel | last post by:
Platform: Windows Vista Language: ASPX / C# I am trying to make a business library for where I work, and I have everything figured out and done except for the ObjectDataSource portion. If any...
15
by: Grey Alien | last post by:
I have a class that contains a std::map variable. I need to export the class via a DLL. the class looks something like this: class MyClass { public: MyClass(); MyClass(const MyClass&); ...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.