Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:28+00:00 2026-06-15T08:14:28+00:00

I have a C++ class library project that is commonly used by other C++

  • 0

I have a C++ class library project that is commonly used by other C++ projects. To be able to use classes inside my class library project, I wrote a header file like the example given below

#pragma once
#ifdef MYLIB
# define MYLIB_EXPORT __declspec(dllexport)
#else
# define MYLIB_EXPORT __declspec(dllimport)
#endif

No problem until I want to create a template class inside my class library project. The problem is I can’t export my template class.

MyClass.h

template<class T>
class MYLIB_EXPORT MyClass
{
    void myMethod();
    // ...
}

template<class T>
void MyClass::myMethod()
{
    // ...
}

In this case I am getting compilaton errors saying “definition of dllimport function not allowed”. I know what causes the problem and I understand it. Other projects using my class library project converts the MYLIB_EXPORT keyword to __declspec(dllimport). Therefore, they are expecting the methods of MyClass to be defined in a DLL. But, then the compiler sees the definition inside the header.

How can I overcome this situation and be able to export my template classes that are defined inside my class library project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:14:29+00:00Added an answer on June 15, 2026 at 8:14 am

    Uninstantiated templates can’t be compiled directly – they are code generators, so they are actually translated to binary instructions only when they are instantiated; for this reason, you can’t export a template “in binary form” as if it was a “regular” function/class (on the other hand, at least in theory you could export an instantiation of a template).

    Long story short: just leave the templates in a header to be included by library clients.

    Notice that this is the exact reason why you keep templates in headers and you don’t normally separate their implementation in .cpp files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class library project contains common codes that used in my projects
I have a .Net 3.5 class library project that I've migrated to use Visual
I have a Class Library project which houses some shared code between other projects
I have a standard class library project with a set of classes that I
I have a class library project that references the version 4.1 Microsoft.Practices.Common dll. I
I have a class, which is part of a code library project that was
Let's say I have a project that is a class library. I have a
I have an ASP.NET VB.NET web project that references a VB.NET class library. I
We have a web project that contain its business methods in a class library
I have a class library project that i have made. Let's call it ClassA.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.