Preprocessor question

General FreeBASIC programming questions.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Preprocessor question

Post by badidea »

Is this possible in some way?

Code: Select all

#define INC_LIB "../_code_lib_new_/"
'const INC_LIB = "../_code_lib_new_/"

'#include once "$(INC_LIB)graphics_v02.bi"
#include once INC_LIB ## "graphics_v02.bi"
Goal: I would like to specify where my .bi files are located via a define.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Preprocessor question

Post by dodicat »

In windows you can define a path.

Code: Select all



#define pathtobi  "C:\Users\User\Desktop\New folder\fbgfx.bi"

#include pathtobi

screenres 400,400,32,,fb.GFX_ALPHA_PRIMITIVES

circle(100,100),100,rgba(200,0,0,100),,,,f
circle(200,100),100,rgba(200,0,0,100),,,,f
sleep


sleep 
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Preprocessor question

Post by badidea »

To clarify, this is how (the top of) my code looks now:

Code: Select all

#include once "../_code_lib_new_/graphics_v02.bi"
#include once "../_code_lib_new_/keyboard_v01.bi"
#include once "../_code_lib_new_/image_v02.bi"
#include once "../_code_lib_new_/image_buffer_v01.bi"
#include once "../_code_lib_new_/string_v01.bi"
#include once "../_code_lib_new_/int2d_v02.bi"
#include once "../_code_lib_new_/sgl2d_v02.bi"
#include once "../_code_lib_new_/event_timer_v01.bi"
#include once "../_code_lib_new_/loop_timer_v01.bi"
#include once "../_code_lib_new_/int2d_sgl2d_v01.bi"
#include once "inc_game/image_enum.bi"
This works fine. "../_code_lib_new_/" is a directory shared by multiple projects. Which I use during development. But when I release / publish the code, I want to copy the relevant files to a directory inside the project, e.g.: "inc_lib/"
Of course I can just do a quick 'search & replace' in the editor, but I was wondering if I can concatenate the 2 text fields (path and file name) to be used in the #include statement.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Preprocessor question

Post by grindstone »

There's indeed a trick to do so.

First you have to rename your include directory to "_ code_lib_new_" with a space between the leading underscore and the rest of the name. Then you can code

Code: Select all

#Define INC_LIB "..\_
'#Define INC_LIB "_

#Include Once INC_LIB code_lib_new_\graphics_v02.bi"
Take care that the line with the #Define ends immediate behind the underscore (no quotes, no following spaces) and also notice that the expression behind the INC_LIB must not have leading quotes.

If anyone knows how to avoid the space between the two parts of the path name, please let us share your wisdom.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Preprocessor question

Post by marcov »

I'm no preproc expert, but maybe the preproc handling of #include once is odd, so try crafting the whole path in a #define with ## and then include the resulting variable.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Preprocessor question

Post by paul doe »

You can also try with something like this:

Code: Select all

'' We need this to force the preprocessor to evaluate the expressions
#define __str__( a ) _
  #a
#define __conc__( a, b ) _
  ##a##b
#define __inc__( p, f ) _
  __str__( __conc__( p, f ) )

#macro __include__( p, f ) _
  #include once __inc__( p, f )
#endmacro

/'
  No quotes, since they'll screw the parser up. Also, note that file names
  that contain spaces also work without issues.
'/
#define INCLUDE_PATH _
  bi/

__include__( INCLUDE_PATH, thread.bi )
__include__( INCLUDE_PATH, brain fart.bi )
Pretty un-BASIC but functional, nonetheless.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Preprocessor question

Post by fxm »

Well done.

I still have trouble remembering the order in which macro calls must be nested for expressions to be correctly evaluated.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Preprocessor question

Post by dodicat »

Sorry Badidea, didn't quite get your request.

You could simply define your path as the folder without the end quote.
and put the end quote at the end of #include.
example

Code: Select all




#define path  "C:\Users\User\Desktop\New folder


#include path\fbgfx.bi"
#include path\document.bi"

using fb


sleep  
Tested on Windows only.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Preprocessor question

Post by badidea »

dodicat wrote:You could simply define your path as the folder without the end quote.
and put the end quote at the end of #include.
That indeed works and is the most simply solution.
The only problem is that it looks ugly. Especially because met editor highlighter gets confused.

Paul doe's version works as well, but also not winning the beauty contest :-)
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Preprocessor question

Post by paul doe »

badidea wrote:
dodicat wrote:You could simply define your path as the folder without the end quote.
and put the end quote at the end of #include.
That indeed works and is the most simply solution.
The only problem is that it looks ugly. Especially because met editor highlighter gets confused.

Paul doe's version works as well, but also not winning the beauty contest :-)
Indeed. As you've probably noticed, it's the quotes that wreak havok on the preprocessor. Mine implements what marcov suggested a few posts above (in fact we cross-posted). But I'd say you should go for the simplest thing that works, looks aren't everything ;)
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Preprocessor question

Post by grindstone »

Very strange.

Code: Select all

#Define INC_LIB "..\_code_lib_new_

#Include Once INC_LIB\graphics_v02.bi"
works, while

Code: Select all

#Define INC_LIB "..\_code_lib_new_\

#Include Once INC_LIBgraphics_v02.bi"
throws an error.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Preprocessor question

Post by badidea »

I expect that "\" or "/" is a special character for the pre-compiler.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Preprocessor question

Post by dodicat »

Off topic.
I also have a preprocessor question.
The help file for #if extract;
This conditional directive differs from the If conditional statement in that #if is evaluated at compile-time and If is evaluated at run-time.

But here in this snippet it looks like #if is being evaluated at run time.

Code: Select all

 

#macro test(x)

#if typeof(x)=double
print "Double"
#elseif typeof(x)=single
print "single"
#elseif typeof(x)=integer
print "Integer"
#elseif typeof(x)=string
print "String"
#endif
print

#endmacro
dim as long k

do
    locate 5
    print "Press 1 = double"
    print "Press 2 = single"
    print "Press 3 = integer"
    print "Press 4 = string"
    print
    print
    do
    if inkey=chr(27) then end
    k=vallng(input(1))
loop until k>0 and k<5
cls
    select case k
    case 1
        test(1#)
    case 2
        test(1!)
    case 3
        test(1)
    case 4
        test(time)
    end select
    loop
    


 
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Preprocessor question

Post by fxm »

If you look at the file.bas after preprocessing, you can see that everything is predisposed at compile time.
file.pp.bas:

Code: Select all

dim as long k

do
 locate 5
 print "Press 1 = double"
 print "Press 2 = single"
 print "Press 3 = integer"
 print "Press 4 = string"
 print
 print
 do
 if inkey=chr(27) then end
 k=vallng(input(1))
loop until k>0 and k<5
cls
 select case k
 case 1
print "Double"
print
 case 2
print "single"
print
 case 3
print "Integer"
print
 case 4
print "String"
print
 end select
 loop
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Preprocessor question

Post by badidea »

Ah, compile with -pp. I learned something new again.
Post Reply