简体   繁体   中英

calling a function declared in header file

I have xh file and yh file, zc file. zc includes yh and yh includes xh I have a function that need to be declared in xh and defined in zc, because it needs to use some other functions in yh

I tried like this

// x.h
int foo();

// y.h
#include "x.h"

// z.c
#include"y.h"
int foo() {
   blah;
   blah;
   return 1;
}

this is all in one project lets say in x_y.dll and it compiled well and i have x_y.dll, x_y.lib

Now in other project a_b in one of the ac file I am trying to include xh and call foo(); compiles well, but I am having the linker error lnk2019 and lnk1120 unresolved external symbols

I am working on VS2008, I have the path set to the folder where x_y.lib located.

Create a macro like so:

#ifndef DLL_IFACE
#ifdef DLL_IFACE_EXPORT
#define DLL_IFACE _declspec( dllexport )
#else  // !DLL_IFACE_EXPORT
#define DLL_IFACE _declspec( dllimport )
#endif // !DLL_IFACE_EXPORT
#endif // !DLL_IFACE

And put this in some header included by everyone. Then in xh:

DLL_IFACE int foo();

then in zc, start that file off with:

#define DLL_IFACE_EXPORT

This will cause the function to be declared "_declspec( dllexport )" when you build the dll that defines the function. And will cause the function to be declared "_declspec( dllimport )" in other dlls.

The dllexport tells the compiler that the given function should be part of a dll's public interface. It put the function in the dll's dynamic symbol table. dllimport tells the compiler that the function will be imported through dynamic linking later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM