AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   Declare one hook in multiple cpp files (https://forums.alliedmods.net/showthread.php?t=332695)

kadet.89 05-27-2021 16:41

Declare one hook in multiple cpp files
 
I need to hook IServerEntity::SetModelIndex founction in multiple cpp files.
I do it this way in the first file:
SH_DECL_HOOK1_void(IServerEntity, SetModelIndex, SH_NOATTRIB, 0, int)
and in the second:
SH_DECL_HOOK1_void(IServerEntity, SetModelIndex, SH_NOATTRIB, 1, int)

I get this error:
PHP Code:

/usr/bin/lderrorfileone.omultiple definition of '__SourceHook_FHAddIServerEntitySetModelIndex(void*, SourceHook::ISourceHook::AddHookMode, bool, fastdelegate::FastDelegate1<int, void>)'
/usr/bin/ldfiletwo.oprevious definition here
/usr/bin/lderrorfileone.omultiple definition of '__SourceHook_FHRemoveIServerEntitySetModelIndex(void*, bool, fastdelegate::FastDelegate1<int, void>)'
/usr/bin/ldfiletwo.oprevious definition here
collect2
errorld returned 1 exit status 

Is there a beautiful way to solve this problem?

RomanPort 12-31-2021 03:16

Re: Declare one hook in multiple cpp files
 
Hello! I just encountered the same problem and searched on this board for help. Surprisingly, this is the only mention I could find. I was stumped on this for a while, but just found the solution: typedefs.

Like you, I had the same hook declared in two different files and I was also getting errors about objects already existing. The reason why is that the SH_DECL_HOOK macro actually declares some variables that you can't see, and there are two with identical names created when you have two of those definitions. Since the name of these variables is derived from the interface name and function, if we can change the name, it'll work fine.

That's where typedefs come in. Typedefs are basically just an alias for a type name. If you set up an alias for the interface in one of the two files with a different name, you can declare the hooks using that new name instead and it won't conflict. It doesn't matter what your alias is as long as it's different from the old name.

At the top of one of your two files, add this line, replacing the two parts of it:

Code:

typedef <EXISTING TYPE NAME> <YOUR NEW TYPE NAME>;
Then, in the rest of just that file, replace any names of the original type (IServerEntity in your case) with your new alias name in any hook-related macros. From there it should compile fine.

tl;dr;

Add this line to the top of ONE of the two files: typedef IServerEntity IServerEntityFileOne;
Then, replace "IServerEntity" with "IServerEntityFileOne" everywhere else hook-related in that one file.


All times are GMT -4. The time now is 07:33.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.