Hi, I need to be able to call a function from all the C++ files included in my project and i am not sure how to do it.. everytime i try something (such as exporting a class or function) i get multiple function declaration errors and it fails to compile, i have been trying to get this to work for the last two weeks and have tried everything, i must be missing something..

my program structure looks like this:
main.cpp - main() - where callfunc() is called for every file..
file1.cpp - __declspec(dllexport) void callfunc()
file2.cpp - __declspec(dllexport) void callfunc()
file3.cpp - __declspec(dllexport) void callfunc()

when my program starts, i want to be able to have the callfunc() function in file1.cpp, file2.cpp, and file3.cpp called after main()

in a nut shell, every C++ file that is included in my project that has the void callfunc() function, i need my program to call it when it starts up so that each file can do its own intalisation

i have tried exporting a class using extern "C" and then the class name and the callfunc() there as a virtual void function, but that fails to work, and i have tried using init() but for some reason the data that is set in the callfunc() function is destroyed after the function finishes.. so that is out of the question

the reason why i want to do this, is for plugin support via source.. and i cant find any good resources on the subject, i am not asking for someone to code it for me, i just dont know what to do... thanks for any help or suggestions in advance.

dllexport and dllimport are only used in DLLs. You don't use those keywords to refer to functions that are contained in other source files that are linked with the project. in main.c you need to prototype the function that is included in a different *.c file using extern keyword. Below is an example how to do this in main.c -- do the same thing in each of the other *.c files.

// main.c file
#include < header files here>
extern void callfunc();

One of the *.c files you link with your program must contain the code for callfunc().

I still get a 'multiple function declaration' error, saying that the function was already declared and it's being declared again..

main.cpp:
#include "main.h"
..
extern void callfunc();
int main(void)
{
callfunc();
return 0;
}

..

file1.cpp:
#include "main.h"
void callfunc() { }

file2.cpp:
#include "main.h"
void callfunc() { }
..

still fails to compile.. no matter where i have extern void callfunc();

>>void callfunc() { }
The function can be coded in one and only one file. All other files the function must be prototyped using the extern keyword.

i know that, in case if you missed it, file1.cpp and file2.cpp are plugins, and i need the callfunc() function for each file it is in (file1.cpp and file2.cpp) to be called by main.cpp so that each file can do its own intalisation when the program starts, hope that makes sense..

for the above comment, this was why i had __declspec(dllexport) void callfunc(), but i realised that each function that is exported must be unique which is why i am getting multiple function declaration errors..

for example, in another programs source that supports plugins i saw this,

someplugin.cpp:
#include "module.h"
void hook_privmsg()
{

}
extern "C" module *init()
{
   module *m = new module("public command", "matrix", "0.1.0");
   m->hooks->privmsg = hook_privmsg;
   return m;
}

keep in mind that this plugin isn't part of a .dll, it's compiled alongside the main .exe as source
and this:

module.h:
..
class module
{
	public:
	pstring<> desc, author, version;
	pstring<> file, md5sum;
	time_t loadDate;
	Hooks *hooks;
	void *(*destroy)();

	module(const char *Desc="", const char *Author="", const char *Version="") :
			desc(Desc), author(Author), version(Version), hooks(new Hooks) {};

	~module()
	{
		destroy();
		delete hooks;
	}
};

then in main.cpp:

init = (module*(*)()) dlsym(handle, "init");
		if(!init)
		{
			_event.setError(this, "error while loading %s: %s", arg2, dlerror());
			return &_event;
		}
		module *m = init();

dlsym is for linux from what i have gathered, and its alternative is GetProcAddress for windows (which is what i am compiling my source on), so i am basically trying to do a similar thing, but like i said, i failed to do it using a class similar to what the program above did because i could never get the plugin to intalise using init..

so i hope that kinda gives you guys a clearer insight of what i am trying to do..

are file1 and file2 in separate DLLs? The term "plugin" is meaningless. Those files are either in a dll, library or part of the application program. Please be a bit more specific about how your program is designed.

If you want to put file1.cpp and file2.cpp in a DLL then you can do one of 3 things that I can think of:
1. use namespaces -- put each instance of callfunc() in a different namespace and export them in the DLL with dllexport, then in the application program using dllimport o import them. There was another recent thread here that has an example of how to do that.

2. put callfunc() as a method of a class.

3. Since file1.cpp and file2.cpp are c++ files, you can create callfunc() with different sets of parameters -- that will prevent duplicate definition errors.

[edit]sorry, I didn't see your second post[/edit]
>>keep in mind that this plugin isn't part of a .dll, it's compiled alongside the main .exe as source
then if callfunc() is NOT in a DLL than you can NOT use dllexport or dllimport.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.