Author Topic: Like the "include" keywords in c language  (Read 6776 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