understanding dll

86 views
Skip to first unread message

marci...@gmail.com

unread,
Nov 11, 2014, 3:44:10 PM11/11/14
to std-dis...@isocpp.org
hi, i am new bie and have problem with dynamic linking in code blocks
i have 2 files:

DLL.dll:

headers:

AFYA_PRODUKT.h:
------------------------------
#ifndef AFYA_PRODUKT_H
#define AFYA_PRODUKT_H
#if OS==1 /* Linux */
#include <dlfcn.h>
#define HINSTANCE void *
#define SHARED_EXPORT extern "C"
#define LOAD_LIB(name) dlopen(name, RTLD_LAZY)
#define LOAD_SYM(lib,name) dlsym(lib,name)
#elif OS==2 /* Windows */
#include <windows.h>
# define SHARED_EXPORT __declspec (dllexport)
#define LOAD_LIB(name) LoadLibrary(name)
#define LOAD_SYM(lib,name) GetProcAddress(lib,name)
#else
#error  OS:  1 (Linux) or  2 (Windows)
#endif

extern "C"
{
 class SHARED_EXPORT   AFYA_PRODUKT
{
public:
    AFYA_PRODUKT();
    virtual ~AFYA_PRODUKT();
    void testT(int t );
};

    SHARED_EXPORT AFYA_PRODUKT*   CK();
};
#endif // AFYA_PRODUKT_H

--------------------
AFYA_PRODUKT.cpp
--------------------

#include "AFYA_PRODUKT.h"
#include <iostream>

extern "C"
{

SHARED_EXPORT AFYA_PRODUKT::AFYA_PRODUKT()
{
    std::cout<<"new AFYA";
}

SHARED_EXPORT AFYA_PRODUKT::~AFYA_PRODUKT()
{
    std::cout<<"del  AFYA";
}
void SHARED_EXPORT  AFYA_PRODUKT::testT(int t)
{

    std::cout<<t;
}
AFYA_PRODUKT* SHARED_EXPORT CK()
{

    return new AFYA_PRODUKT();
}
}


i compile it with additional data -DOS=2  //windows



and my test main.cpp file:

#if OS==1 /* Linux */
#include <dlfcn.h>
#define HINSTANCE void *
#define SHARED_EXPORT extern "C"
#define LOAD_LIB(name) dlopen(name, RTLD_LAZY)
#define LOAD_SYM(lib,name) dlsym(lib,name)
#elif OS==2 /* Windows */
#include <windows.h>
#define SHARED_EXPORT __declspec(dllexport)
#define LOAD_LIB(name) LoadLibrary(name)
#define LOAD_SYM(lib,name) GetProcAddress(lib,name)
#else
#error OS: 1 (Linux) or 2 (Windows)
#endif
#include <iostream>
#include ".....\cpp\afya_produkt\include\AFYA_PRODUKT.h"

using namespace std;

int main()
{

    HINSTANCE hDll;
    hDll = LOAD_LIB("libafya_produkt");
    typedef AFYA_PRODUKT*(  *pA)();

    pA Fx= reinterpret_cast<pA> (LOAD_SYM(hDll, "CK"));
    AFYA_PRODUKT* PR = Fx();
    int i=7;


///// and here i  become error, code::block shows, that i have function testT in my AFYA_PRODUKT class\\
but i can not compile program.... until i comment it, after this i comple program and see in normal in console my written constructor (new afya and del afya)

//    PR->testT(i);




    delete PR;

    return 0;
}


WHAT DO i wrong?

Daniel Krügler

unread,
Nov 11, 2014, 3:48:01 PM11/11/14
to std-dis...@isocpp.org
2014-11-11 21:44 GMT+01:00 <marci...@gmail.com>:
> hi, i am new bie and have problem with dynamic linking in code blocks
> i have 2 files:

This is not the right newsgroup for this kind of questions. This
newsgroup discusses Standard C++. Dlls and other shared libraries are
not covered by the C++ Standard.

Thanks,

- Daniel
Reply all
Reply to author
Forward
0 new messages