Author Topic: Like the "include" keywords in c language  (Read 6775 times)

0 Members and 1 Guest are viewing this topic.

jackyieman

  • Guest
Like the "include" keywords in c language
« on: February 10, 2015, 09:31:56 AM »
Hello everyone!
These days I have try to modify my lisp code.
But I found that I have used to using c language , I want to make the code modularization.
Just like :
#include <stdio.h>

So that I could part the code to someone file, when I need it , I can declare it.
And I can place the sub routine to another file.

But I dont know how to get it , could anyone else help me?  Thanks!

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: Like the "include" keywords in c language
« Reply #1 on: February 10, 2015, 09:49:03 AM »
You're posting in the wrong forum!  Try General Programming (was C++)
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

jackyieman

  • Guest
Re: Like the "include" keywords in c language
« Reply #2 on: February 10, 2015, 10:00:47 AM »
You're posting in the wrong forum!  Try General Programming (was C++)

Sorry maybe i should said that:
If i have 10 sub routines like
(defun routine1()
  ....
)


...

(defun routine10()
 ...
)


but i dont want to place the code in same file, i want to save the file saperately.
just like below:

main.lsp
subroutines.lsp
parameters.lsp
algorithm.lsp

but how to link with all the file and how to declare functions in other files?
Thanks!

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Like the "include" keywords in c language
« Reply #3 on: February 10, 2015, 10:13:12 AM »
No linking needed.  (defun ...) 's are available everywhere - they have no scope.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Like the "include" keywords in c language
« Reply #4 on: February 10, 2015, 10:18:33 AM »
Don't you guys load the files in acaddoc.lsp or something like that?

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: Like the "include" keywords in c language
« Reply #5 on: February 10, 2015, 10:58:54 AM »
That's a lisp question!  Not sure yet what you're looking for though.  There are many ways to load lisp routines.  Acad.lsp will load them when AutoCAD opens.  Acaddoc.lsp will load them when each drawing opens.  Most routines that aren't used as often can be auto loaded as needed.  Many ways to do that.  Are you going to type the commands or put them in the CUI as macros?
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

jackyieman

  • Guest
Re: Like the "include" keywords in c language
« Reply #6 on: February 10, 2015, 11:09:40 AM »
That's a lisp question!  Not sure yet what you're looking for though.  There are many ways to load lisp routines.  Acad.lsp will load them when AutoCAD opens.  Acaddoc.lsp will load them when each drawing opens.  Most routines that aren't used as often can be auto loaded as needed.  Many ways to do that.  Are you going to type the commands or put them in the CUI as macros?

I think it just type the commands not put them in the CUI.

I am making the little function about processing the drawing in ACAD and output the result to excel files.
But when i finish the program , i think assamle all the code in one file is not good idea.
So that i want to replace some functions like general function, parameters ,.etc into other files.
Because it would be much clear in every time i edit it.

My idea is that:
when i load the main program, then load the parameters.
But some function or parameters would not be loaded until i called.

Thanks!

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Like the "include" keywords in c language
« Reply #7 on: February 10, 2015, 01:00:49 PM »
Perhaps this utility by John Kaul (Se7en) may be of interest.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Like the "include" keywords in c language
« Reply #8 on: February 10, 2015, 01:02:38 PM »
I was just trying to find that utility and post a link when you did Lee.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Like the "include" keywords in c language
« Reply #9 on: February 10, 2015, 05:07:41 PM »
That's a lisp question!  Not sure yet what you're looking for though.  There are many ways to load lisp routines.  Acad.lsp will load them when AutoCAD opens.  Acaddoc.lsp will load them when each drawing opens.  Most routines that aren't used as often can be auto loaded as needed.  Many ways to do that.  Are you going to type the commands or put them in the CUI as macros?

I think it just type the commands not put them in the CUI.

I am making the little function about processing the drawing in ACAD and output the result to excel files.
But when i finish the program , i think assamle all the code in one file is not good idea.
So that i want to replace some functions like general function, parameters ,.etc into other files.
Because it would be much clear in every time i edit it.

My idea is that:
when i load the main program, then load the parameters.
But some function or parameters would not be loaded until i called.

Thanks!

You're working with LISP, not C or a C-derivative, so start thinking LISP.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Like the "include" keywords in c language
« Reply #10 on: February 10, 2015, 05:32:37 PM »
About 15 years ago  Reini Urban had development control of a Standard AutoLISP Library STDLIB
which had a REQUIRE functionality ... the idea was to ensure that all components for an application were loaded if needed.

It used this sort of mechanism
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (if (std-acad-connection-p)
  3.    (std-require "STDINPUT")
  4.    (std-require "STDTBL")
  5.    (std-require "STDENT")
  6.    (std-require "STDMISC")
  7.    (std-require "STDERROR")
  8.    (std-require "ENTMAKE")
  9.  )
  10. )
  11.  

Sounds like this is what you are requiring.

You may still be able to find the source on the web.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Like the "include" keywords in c language
« Reply #11 on: February 11, 2015, 01:19:21 AM »
In short. The effect of an include statement in C can be duplicated using a load call in Lisp. So if you had something like this:
Code - C: [Select]
  1. #include "path/to/my/lib_source.c"
  2. // Continue with your code
  3. some_function_in_lib();
  4. // Further code

The exact same result can be gotten in Lisp by doing the following:
Code - Auto/Visual Lisp: [Select]
  1. (load "path/to/my/lib_source.lsp")
  2. ;; Continue with your code
  3. (some_function_in_lib)
  4. ;; Further code

Hello everyone!
These days I have try to modify my lisp code.
But I found that I have used to using c language , I want to make the code modularization.
Just like :
#include <stdio.h>

So that I could part the code to someone file, when I need it , I can declare it.
And I can place the sub routine to another file.

But I dont know how to get it , could anyone else help me?  Thanks!

Just to clear up a few points:
  • Many talk about including such as "static linking", though strictly speaking that's not true with C/C++. The include directive (note it's a pre-processor directive, not a compilable statement) in C and C++ doesn't actually "load" a library. It's more like copy-pasting the library into the current source file. This tends to be the biggest reason why large C/C++ programs take sooooo long to compile. Because they're re-compiling the same code hundreds of times over - for each include statement in every single source file.
  • Lisp works more along the lines of Java/Python in this case, a slight difference (see next points). Instead of duplicating the include it simply loads it into shared memory. Then any calls to functions in that library simply calls the already loaded function instead of reloading it. The effect is the same, but it only needs to load the library code once to be available to all of your source. So replace the "include" directive with a (load "filename") call somewhere (anywhere) in your code.
  • Lisp does dynamic linking, i.e. it loads the code at runtime. Static linking is when the library is compiled to an object file then linked into the final executable - this is why C's include is nothing to do with "linking" in any way (it's just a copy-paste). You can static link pre-compiled object files in most C compilers, but that's not what include does. Other environments (even C's) can do dynamic linking also, think loading DLL files at runtime.
  • Lisp (especially AutoLisp) has 2 reasons why any loaded library is available to everything at once, even if it was loaded by some other lisp file. (1) There's a single global shared namespace per drawing (unless you've made a separate namespace VLX/FAS). (2) The AutoLisp dynamic scoped environment basically places all variables, and thus also all functions, as "global" variables - localized variables simply mean the global is cleared once the function completes. This tends to be very different from most other language environments, e.g. with Java/C# you have clear groupings of namespaces and need special treatment to work with values and functions in outside namespaces. Also the lexical scope (well mostly) would mean that any local variable is only ever available to the actual function which generated it, it's never visible outside even while the function is running.
  • Actually this global scope of "included" C source files acts very similar to Lisp's global dynamic scoping, at least when referring to such pre-processor include directives in comparison to Lisp's load function. It's one of the issues some are asking the C compilers to sort out, without any such luck for some decades now. I.e. localize includes only in the actual C source file where they're loaded, or alternatively (if still loading into the global namespace) don't reload each include ad infinitum (check if loaded already and ignore when found). Actually this is one of several reasons so many "new" languages have cropped up - to try and repair this "issue" in C/C++.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #12 on: February 11, 2015, 10:28:27 AM »
About 15 years ago  Reini Urban had development control of a Standard AutoLISP Library STDLIB
which had a REQUIRE functionality ...

Reini's code, most of it a masterpiece, was a mess! I looked into picking up maintenance to it but I quickly discovered it would have been an absolute nightmare to maintain. ...However that STLIB is a great piece of work; a lot to learn from in it!

In short. The effect of an include statement in C can be duplicated using a load call in Lisp. ...

Don't do that (it creates a spaghetti mess).

When I started thinking about creating my own STDLIB (it was going to my last contribution to the community) and spent some time with Reini's lib I realized that it's not always as easy as `cut and paste' or "use that function" here. Reini's lib had duplicate/different functions for the same tasks, which created a problem with names, [-i.e. function names were confusing and only slightly different] and my lib was developing the same problems. ...LiFP was born. The `cut and paste' portion of LiFP is great but the real power comes from the preprocessor directives [-i.e. IFDEF, IFNDEF, etc.] where the programmer can build in intelligence and elegance to their code/library functions.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

d2010

  • Bull Frog
  • Posts: 326
Re: Like the "include" keywords in c language
« Reply #13 on: February 11, 2015, 12:34:32 PM »
The users autolisp   can  develope  "autolisp programs" 
with C/C++ syntax and algorithms.. You see here.,..

http://www.theswamp.org/index.php?topic=39246.msg444641#msg444641

If you compile "pp_utmcoosmall.cpp"  ,then you got "pp_utmcoosmall.lsp"..
Why the user's  autolisp  need C+/C/skill?
Because1, in the future they  must learn  C#/ dot.NET / Java /Java on Cloud ..

Thanks...
« Last Edit: February 11, 2015, 12:47:29 PM by d2010 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #14 on: February 11, 2015, 01:03:04 PM »
The users autolisp   can  develope  "autolisp programs" 
with C/C++ syntax and algorithms.. You see here.,..

http://www.theswamp.org/index.php?topic=39246.msg444641#msg444641

If you compile "pp_utmcoosmall.cpp"  ,then you got "pp_utmcoosmall.lsp"..
Why the user's  autolisp  need C+/C/skill?
Because1, in the future they  must learn  C#/ dot.NET / Java /Java on Cloud ..

Thanks...

I checked the file attached and that is not a CPP file.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Like the "include" keywords in c language
« Reply #15 on: February 11, 2015, 07:07:39 PM »
What is the big deal with having an include directive in lisp?

The way I see it is you simply load the thing ....
Code: [Select]
(load "required.lsp")

If you want to make it "feel" like C then write a directive function and put it in the acaddoc.lsp ...

Code: [Select]
(defun include (fn)
  (load fn)
)

then in your other lsp files use it like this:
Code: [Select]
(include "this.lsp")

easy peasy
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Like the "include" keywords in c language
« Reply #16 on: February 11, 2015, 07:56:23 PM »
This is a slightly more robust loader.


Code - Auto/Visual Lisp: [Select]
  1. ;;(include <"CommandName"> <"fileName">)
  2. ;;
  3. (defun include (cmdString fn)
  4.    (cond ((eval (read cmdString)))
  5.          (t (load fn (strcat "\nUnable to load file " fn)))
  6.    )
  7.    (princ)
  8. )
  9.  
  10.  


Code - Auto/Visual Lisp: [Select]
  1. (include "Test_42" "TestCode.lsp")
  2. ;;
  3. ;;
  4. (test_42)
  5.  

Test Routine:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;; File TestCode.lsp
  3.  
  4. (defun test_42 ()
  5.    (prompt "\nFourty")
  6.    (prompt " two")
  7.    (princ)
  8. )
  9.  
  10.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Like the "include" keywords in c language
« Reply #17 on: February 12, 2015, 03:22:02 AM »
In short. The effect of an include statement in C can be duplicated using a load call in Lisp. ...

Don't do that (it creates a spaghetti mess).
That is EXACTLY why the C's include directive is a mess. It creates such spaghetti code all over the place. I.e. to answer the OP ... to get the EXACT same thing as C's include directive, just load the other file - the same thing happens: load instead of copy+compile.

If you want something like a Common Lisp's requires function ... that's not what include does in C, so it's not what the OP asked for. Even though it's a much better alternative.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #18 on: February 12, 2015, 09:38:09 AM »
The INCLUDE statement does not increase compile time; it speeds it up. INCLUDE does not make spaghetti code; it keeps code more organized.

...Having the same/slightly modified/updated support function copied in several files makes for a huge mess; one steps on the others toes or undoes something needed by this version. Support functions get copied and pasted and get updated but no one goes back through all their files and updates all of the function's instances.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Like the "include" keywords in c language
« Reply #19 on: February 12, 2015, 10:52:52 AM »
Does AutoLisp have a "last one loaded wins" approach?

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #20 on: February 12, 2015, 10:59:08 AM »
Does AutoLisp have a "last one loaded wins" approach?
Yes. ...Some times it can be a problem when a support procedure--that uses the same name but has been altered--localizes or changes global vars different then the one the procedure is expecting or had. This comes into play a lot when old/unmaintained code is used and/or loaded in the incorrect order.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Like the "include" keywords in c language
« Reply #21 on: February 13, 2015, 02:07:40 AM »
The INCLUDE statement does not increase compile time; it speeds it up. INCLUDE does not make spaghetti code; it keeps code more organized.

...Having the same/slightly modified/updated support function copied in several files makes for a huge mess; one steps on the others toes or undoes something needed by this version. Support functions get copied and pasted and get updated but no one goes back through all their files and updates all of the function's instances.
Four points in response:

(1) I'm assuming you are talking about the C programming language, or perhaps even the C++ language. NOT the C# language, which has an entirely other way of going about such includes. If I'm wrong in this, then disregard everything I've posted about it - we're talking about apples and pairs if so.

(2) As I understand the "spaghetti" code: it's that you end up with lots of inter-dependent source files which fail if only one of them is missing. Or even worse, when some function in one is overridden in another to do something else. This is exactly the same thing which occurs in C's include - see below.

(3) A C include statement is nothing else than a "copy that file into this spot before compiling". This means that if you #include FileA into FileB, then it's as if you opened FileA, selected all, Ctrl+C, swapped to FileB and Ctrl+V. If you don't believe me, see the answers from experts:
http://stackoverflow.com/questions/5735379/what-does-include-actually-do
http://www.cplusplus.com/forum/articles/10627/
http://stackoverflow.com/questions/1539347/what-are-ways-of-improving-build-compile-time
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4022.html

(4) So when you have FileA "included" into more than just one single other file, the compile-time will get increased because it will literally compile the same code more than once (note literal meaning of the word literally). This gets worse when you're including a file which includes other files on its own. You can very easily have a source file only a few lines long, including one or two others, which then expands through the pre-processor into 1000's of lines of source code for the compiler to handle. And it's worse in C than in Lisp's last-loaded-wins approach, because the compiler may compile any file in any order, especially as it may be set to compile using multiple threads - so you cannot even say for sure which "version" of a function will be the final one which is in the compiled result.

There's actually a way to try and avoid this issue in C. It's called "include guards": http://www.cplusplus.com/forum/general/71787/

A similar approach in Lisp would be to check if a function is already defined before loading it, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (if (not MyFunc)) (load "MyFile"))
This does the same as a C construct like this: The file to be included needs to define a pre-processor definition to enable other files to check
Code - C: [Select]
  1. #ifndef _MY_FILE_ //Only define if it's not already defined
  2. #define _MY_FILE_ //Define the pre-processor directive
  3. #endif // End of if block
  4.  
  5. // The rest of the code inside the MyFile.C/H source

Then in the other files you do the following:
Code - C: [Select]
  1. #ifndef _MY_FILE_ // Check if the file's not already included somewhere else
  2. #include "MyFile.C/H" // Copy the file into this spot
  3. #endif
  4.  
  5. // Rest of code for this other file
To me the Lisp way is a lot less convoluted, it's a simple one-liner (strictly speaking 2 lines, if and load), you don't need to edit the file to be loaded, and all you need to know about it is one of its defun names (which you should already since you want to use them). Even if you try to do the exact same in C it gets very hairy: http://stackoverflow.com/questions/6916772/can-i-re-define-a-function-or-check-if-it-exists
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #22 on: February 13, 2015, 08:41:29 AM »
One acronym response (read as humor; I'm not trying to be an a55).
LiFP
I built all of that, and more into LiFP. I'm sure you can set up all of those/the methodologies with plain alisp. Reini already did; use his like Kerry suggested (Why redo something that is already done/can work for you) but I really tried to bring a a more robust development methodology to autolisp then "load".

Honest question: how many times have you written "getpoint", "begin undo", "error", etc.? ...When I was writing in lisp I must have wrote those generic functions a thousand times. I imagine that I copied them quite a bit but I would venture a guess that I just re-wrote more often than not. That was a mess; I had several versions of the same function in several different programs (with slightly different names too--"get_point", "get-point", "myGet-Point"-). Later I got more organized and created utility files but that just eventually created a maintenance mess (my paths had to be kept up as well as the files and functions in them). This also created bloat (like what I think you're referring to with c#). ...not to mention, trying to compile with that method was worthless.

When I set up LiFP I hid the include statement and preprocessor directives behind a comment character so the programmer can chose either method (preprocess or load) if they choose or wanted to set it up. I also built in a simple include guard so there wasn't any of that headache associated. I also made LiFP work with makefile so tasks could be automated [-i.e. like creating packages or updating network deployments or fetching code from or to the internet. ...automation].

I put a lot of work into designing LiFP and into it's support [-e.g. I built a sample stdlib someone could use as a starting point for their own. I made sure I included examples of how to use it with makefile. I even showed how you can use it to build a webpage.].

Now, all that said, do I think the OP, you, or anyone besides the few users that already do will actually try LiFP? No; Autolispers just want to open, write, and load. They do not want to think about any of the problems associated with having a lot of files/code to maintain or how to deploy their code to the network or even work in a group. They don't care or want to think about organization (put code in a file, put that file in a folder[maybe], load and go).

I know/`believe you'/understand how C's include statement works.
I know what an include guard is. ...BTW, step into the 1990's and use "#pragma once".
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8723
  • AKA Daniel
Re: Like the "include" keywords in c language
« Reply #23 on: February 13, 2015, 09:22:31 AM »
I used #pragma once

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Like the "include" keywords in c language
« Reply #24 on: February 13, 2015, 09:28:26 AM »
lol
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Like the "include" keywords in c language
« Reply #25 on: February 14, 2015, 02:58:48 AM »
I'm no saying that standard AutoLisp's load is very good. It has lots of problems. I'm just saying that it's nothing worse than C's include.

I'm sure that something like LiFP is a much better alternative to any of ALisp, C or C++ 's standard multi file handling methods. Though as I see it (note I've not used LiFP much) it's a lot closer to a full macro preprocessor, much like the one usually found in C or C++. If I'm not mistaken then it's not going to help a lot in keeping someone from writing spaghetti code. The spaghetti code is more a symptom of the language itself ... I.e. it allows the same function to be redefined without adequate (or even any warning). Whereas something like the enforced name spaces in C# or Java would disallow any such repurposing of a function's name just willy nilly.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.