Steinfeld
Steinfeld

Reputation: 699

LNK2019 on a solution with a dll project

I am trying to create a solution which one project is the .exe and the other project is a simple dll. What i am trying to learn is how to link between two projects. I have searched stack-overflow and found really nice answers which I have followed, such as declaring the right header bath on:

Properties->Configuration Properties->C/C++->General->Additional Include Directories

Then setting the .lib on:

Properties->Configuration Properties->Linker->Input->Additional Dependencies

I used macros to generate that .lib file also. Here is the my simplified code:

The .exe: cpp:

#include "stdafx.h"
#include "../ConsoleApplication2/HelloWorld.h"

int _tmain(int argc, _TCHAR* argv[])
{
    hello_world hw;
    hw.printHello();
    getchar();
    return 0;
}

The dll: header:

#pragma once
#ifdef is_hello_world_dll
#define hello_world_exp  __declspec(dllexport)
#else
#define hello_world_exp  __declspec(dllimport)
#endif


class hello_world_exp hello_world
{
public:
    hello_world();
    ~hello_world();
    void printHello();
};

cpp:

#include "stdafx.h"
#include "HelloWorld.h"
#include <iostream>

hello_world::hello_world()
{
}

hello_world::~hello_world()
{
}

void printHello()
{
    std::cout << "Hello World" << std::endl;
}

A note: The solution compiles fine when I don't call hw.printHello(); however when I do call it, the linker generates :

Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall hello_world::printHello(void)" (__imp_?printHello@hello_world@@QAEXXZ) referenced in function _wmain C:\Users\usteinfeld\Desktop\Private\Students\Yana\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1

Upvotes: 1

Views: 131

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 118011

This function is defined as a free function based on how you wrote it

void printHello()

It belongs to the class hello_world so you should scope it as such

void hello_world::printHello()
{
    std::cout << "Hello World" << std::endl;
}

Upvotes: 3

Related Questions