- NSIS Discussion
- multiple files - multiple includes and defines - confusion
Archive: multiple files - multiple includes and defines - confusion
Sheik
10th June 2008 21:10 UTC
multiple files - multiple includes and defines - confusion
Hi all,
There has to be a standard way to do this, but I just can't find it...
Its the standard multiple include/define problem that you see in C, where I am getting things multiply defined/included.
I am trying to split up my installer source code to have multiple source files, very much like MUI2 does with its Pages.
Lets say I have main.nsi, file1.nsh, file2.nsh and file3.nsh
main.nsi includes file1.nsh, file2.nsh and file3.nsh
Now, lets say that file1.nsh needs to use StrTrimNewLines... So I include StrFunc.nsh...
Like:
!include "StrFunc.nsh"
${UnStrTrimNewLines}
Thats fine...
But oh wait, now I also need to include it in main.nsh as well.
If I try to do the include again there, I get errors. Presumably this is because I am including and defining a function twice...
Okay, so perhaps I can do the usual trick under C, which is to check to see if something is defined, and only include the header file if its not defined yet...
However, this doesn't seem to work either...
What is the typical way to solve this problem with NSIS?
Yes, I know I could include it just once in main.nsi, and then include my file1.nsh and file2.nsh files below that... But I don't want to do that, because then file1.nsi and file2.nsi isn't "standalone", but instead requires someone above it to include things for it... Which I don't want...
Scott
Comperio
10th June 2008 22:42 UTC
I usually define something in the beginning and then wrap the entire NSH code inside a !if statment. (Many of the included files with NSIS do the same.)
Use File1.nsh as an example. You'd do something like this:
!ifndef File1_Included
!define File1_included
...
[your code here]
...
!endif
Sheik
11th June 2008 01:18 UTC
Yeah, I tried that as well, but the problem is that the macro's are actually being inserted in both files.
Thus it ends up having the same function defined twice!
Like my example above:
If I do this at the top of file1.nsh:
!include "StrFunc.nsh"
${UnStrTrimNewLines}
Which I have to do, because this file actually requires the UnStrTrimNewLines function.
So this ends up inserting the function into file1.nsh.
But what happens if I need the same function in main.nsi ?
If I do the:
!include "StrFunc.nsh"
${UnStrTrimNewLines}
This will insert the function yet again, but this time into my main.nsi file.
So now, if main.nsi !include's file1.nsh, I have a duplicate function name of UnStrTrimNewLines...
How the heck do you resolve this problem?
Afrow UK
11th June 2008 09:46 UTC
Bit ugly but should work:
!include StrFunc.nsh
!if `${UnStrTrimNewLines}` == `!insertmacro FUNCTION_STRING_UnStrTrimNewLines`
${UnStrTrimNewLines}
!endif
Stu
Sheik
11th June 2008 15:36 UTC
Hi Stu,
Wow! That actually works PERFECTLY!
Thanks for your help on this one, I was expecting to have to spend the greater part of today trying to find a way around this problem, and you just solved it for me in like 2 minutes! =)
Thanks again!
Scott
Comperio
11th June 2008 15:51 UTC
ARGH! I didn't realize that StrFunc was so messy! :eek:
While Afrow's solution works ok, I could see that getting very tedious if you had to use more than just that one function from StrFunc.nsh.
Personally, I prefer using FileFunc.nsh or WordFunc.nsh instead. You get pretty much the same functionality as you would using StrFunc, but it's much easier to work with--especially when using multiple includes as in your example.
Afrow UK
11th June 2008 16:08 UTC
The simple way to find the value of ${UnStrTrimNewLines} (and any others) for comparison is to do !error `${UnStrTrimNewLines}` at the top and then copy its value from the makensis log.
Stu
********
16th September 2008 13:59 UTC
!ifndef StrTok_INCLUDED
${StrTok}
!endif
But it is still a bad solution for header files, because if the header file with ${StrTok} definition is included before ${StrTok} is used in main file - it will cause errors.
I bet many people will struggle with the same problem with officially distributed StrFunc.nsh
How about to add this info in header or deprecate ${StrTok} calls in favor of usual !insertmacro ?