HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[code.haxe.org] Compilation - Compiling libraries without main class #37

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

Compiling libraries without main class - Compilation - Haxe programming language cookbook

In most cases, you want a Main class with a static main function as entry point to start your program. However, there are cases where there is no need for this - for example if you are writing a library that other programs will be using.

https://code.haxe.org/category/compilation/compiling-libraries-without-main-class.html

opyate commented 4 years ago

The line

-js bin/lib.js # <- Compile the library, in this case to JavaScript

If this is a library, why specify a compilation target? Isn't it up to the library user to choose a target?

Aurel300 commented 4 years ago

@opyate You are thinking of a Haxe library (like the packages on haxelib), which is not what this article describes.

A Haxe library can be used with -lib when compiling, and will generally result in more classes and packages being available to use from within your Haxe code. These are then all compiled alongside with your code into whatever the target is.

On the other hand, almost all of Haxe targets have a concept of a library as well. To continue with the JavaScript example presented in the article, you can compile some Haxe code to a JavaScript library (making sure to @:expose the classes you want), then you can write regular JavaScript code making use of the compiled library. This is important to allow more types of interop between Haxe code and its targets.

projectkit commented 4 years ago

Can I expose Haxe classes to a Jar library to use it in Java code?

RealyUniqueName commented 4 years ago

I think Haxe-generated classes should be available by the same package/name they use in Haxe.

limenleap commented 4 years ago

I seem to have followed your instructions along with your example but the Haxe compiler INSISTS on not working.

It ALWAYS gives this error :-)

Type not found: foo.BarLib

I tried the following:

I wrote your code into a file called foo.hx

What do I do? I need this feature!

limenleap commented 4 years ago

The file foo.hx was placed in the src sub-folder (i.e. folder where haxe executable is present) I even tried capitalizing it as Foo.hx but NOTHING works. It always keep giving the error

Type not found: foo.BarLib

I even tried placing @keep above the BarLib class. Even that did not work

limenleap commented 4 years ago

I mean @:keep (sorry for my typo)

I tried the file name where you code was written, as per the following list and placed the file in the src folder as explained earlier. None of them worked. They ALL generated the same "Type not found" error

foo Foo Foo.hx Foo.ds foo.ds Foo.pack foo.pack

I even ensured that there is ONLY one source code file (with your code) in the src folder. But even then it did not work

I am using Haxe 4.1.1

RealyUniqueName commented 4 years ago

Each package denotes a directory. You need BarLib.hx file to be placed in foo directory.

limenleap commented 4 years ago

Ah, thank you! How I wish this was properly documented! I wish there was some more info on this. Is there any page in the documentation I may have missed?

RealyUniqueName commented 4 years ago

Looks like we're missing a page about packages in the manual.

limenleap commented 4 years ago

Thanks for all the help. Now how do I actually use the BarLib class ? It seems to add foo_ before it; but since the entire lib.js is actually one big IIFE, and there are no global variables; can you give a small example where the javacript generated can actually be used? Thanks, I am grateful

limenleap commented 4 years ago

I am summarizing what I have learnt here. This could help others. I am using Haxe 4.1.1

What is a package? A package name is actually the name of a sub-folder within the folder given by -cp parameter in the .hxml file

So if ./src is the folder you defined the class path to be, and "foo" is the name of the package then you must have this folder inside your computer (not in the build.hxml file), thus

./src/foo

The buid.hxml is now this. If you note you have to give -cp folder as src and not src/foo !

-cp src

--dce no

foo.BarLib

-js bin/lib.js

If you now want to place the class BarLib defined in your haxe source code; then the sample code shown above on this page should be placed inside a file called BarLib.hx and that file must be in src/foo folder

I have specified the dead code elimination directive to be "no" -- this can create a very verbose file; but you are sure that whatever you wrote in the BarLib.hx would get compiled

Also, ensure that right at the top of that file (BarLIb.hx) should be the special keyword that define it to be the package. The package name should be same as the package folder name

package foo;

Then just above the BarLib class definition; write this:

@:expose

If that is not given, haxe will build the library; but you may not be able to access that class you want

Now compile using haxe as before:

haxe build.hxml

The file lib.js would be created in the bin folder. To get the exposed class; your additional code that you will use, would access your BarLib class as shown below: (in javascript. Other targets, will have similar but may not be exactly same strategies of using a namespace)

foo.BarLib

danielo515 commented 1 year ago

And here we are, two years later, and this blog post is impossible to understand and follow without the comments of @limenleap. Thank you mate for sharing your findings, I wasted a lot of time on this, but at least now I am able to run it.