HaxeFoundation / hashlink

A virtual machine for Haxe
https://hashlink.haxe.org/
MIT License
812 stars 158 forks source link

Problems with compiling the hl/c output. #86

Closed ghost closed 6 years ago

ghost commented 7 years ago

hi,

I cannot compile the hl/c output of this simple hello.hx:

class Hello {
    static function main() {
        trace("Hello World!");
    }
}

Compile to hl/c works:

$ haxe -hl hello.c -main Hello

Now comes the part, where the tutorial is not working anymore:

gcc -o hello -D INCLUDE_ALL -std=c11 hello.c -Lhl

This gives the error:

hello.c:2:10: error: 'hl/code.h' file not found with <angled> include; use "quotes" instead
#include <hl/code.h>
         ^~~~~~~~~~~
         "hl/code.h"
In file included from hello.c:2:
./hl/code.h:6:10: fatal error: 'hlc.h' file not found
#include <hlc.h>
         ^~~~~~~
2 errors generated.

After copying some files from ./src like hl.h, hlc.c, hlc.h, hlc_main.c, there are no missing errors when I include the 'hl' directory and the root directory '.':

gcc -o hello -D INCLUDE_ALL -I hl -I . -std=c11 hello.c -Lhl

But then get a lot of 'Undefined symbols' error, a sign that the library probably cannot be found.

So what to do for this simple hello world?

Undefined symbols for architecture x86_64:
  2   "_hl_add_root", referenced from:
  3       _hl_init_roots in hello-e72367.o
  4   "_hl_alloc_array", referenced from:
  5       _String_split in hello-e72367.o
  6       _hl_types_ArrayDyn_concat in hello-e72367.o
  7       _hl_types_ArrayDyn_copy in hello-e72367.o
  8       _hl_types_ArrayDyn_map in hello-e72367.o
  9       _hl_types_ArrayObj_concat in hello-e72367.o
 10       _hl_types_ArrayObj_slice in hello-e72367.o
 11       _hl_types_ArrayObj_splice in hello-e72367.o
 12       ...
 13   "_hl_alloc_bytes", referenced from:
 14       _String_charAt in hello-e72367.o
 15       _String_substr in hello-e72367.o
 16       _String_fromCharCode in hello-e72367.o
 17       _String___add__ in hello-e72367.o
 18       _hl_types_ArrayDyn___cast in hello-e72367.o
 19       _hl_types_ArrayBytes_Float_concat in hello-e72367.o
 20       _hl_types_ArrayBytes_Float_copy in hello-e72367.o
 21       ...
 22   "_hl_alloc_closure_ptr", referenced from:
haxiomic commented 7 years ago

Hey @bentxt,

If you're getting to the linker stage then you're nearly there. You're right that the missing symbols indicate the hashlink library isn't being linked.

Have you compiled a libhl.dylib (macOS) or libhl.so (linux)?

If not run make libhl in the root of this repository

Once you've got a libhl.*, run make install_lib to copy it to the system lib search path (/usr/local/lib on macOS), then you can add -lhl to your gcc command and it'll link with the libhl.* you built.

(If you're still getting undefined symbols then check the top of your gcc output for anything like cannot find library hl and try to figure out why the compiler can't find it)

At the moment you've got a -Lhl flag which adds a library search path to the gcc command which might not be what you want. Lowercase 'L' is needed to specify a library to link with

I think automatic native compilation is still in the pipes so for the time being it requires a bit of figuring out how to compile it yourself

haxiomic commented 7 years ago

Additionally, you can resolve your first issue about missing headers by adding an include flag to the GCC command- like -I hashlink/src (but substitute your own path to hashlink repo)

ghost commented 7 years ago

Thanks for your help. Let me document the whole process. Maybe I put it somewhere.

Run a hello world with the C backend of Hashlink:

Requirements: haxelib install hashlink

git clone https://github.com/HaxeFoundation/hashlink.git
cd hashlink
make
sudo make install_lib

then leave the project: cd .. then with hello.hx:

class Hello {
    static function main() {
        trace("Hello World!");
    }
}

Compile to c with haxe (hashlink) and then to native with gcc:

haxe -hl hello.c -main Hello

gcc -o hello -D INCLUDE_ALL -I .  -I hashlink/src  -std=c11 hello.c -lhl

./hello
Hello.hx:3: Hello World!/hello

System information:

haxe -version
4.0.0 (git build development @ a018cbd)

gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
ncannasse commented 7 years ago

Yes I need to update the website, the instructions are outdated because they were for 3.4 and we changed things for 4.0 Will maybe wait a few more weeks

On Tue, Oct 24, 2017 at 12:43 PM, bentxt notifications@github.com wrote:

Thanks for your help. Let me document the whole process. Maybe I put it somewhere. Run a hello world with the C backend of Hashlink:

git clone https://github.com/HaxeFoundation/hashlink.git cd hashlink make sudo make install_lib

then leave the project: cd .. then with hello.hx:

class Hello { static function main() { trace("Hello World!"); } }

Compile to c with haxe (hashlink) and then to native with gcc:

haxe -hl hello.c -main Hello

gcc -o hello -D INCLUDE_ALL -I . -I hashlink/src -std=c11 hello.c -lhl

./hello Hello.hx:3: Hello World!/hello

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/HaxeFoundation/hashlink/issues/86#issuecomment-338949782, or mute the thread https://github.com/notifications/unsubscribe-auth/AA-bwFvbbq9lygYpzmiFrw0gkr1EX-Ucks5svb9ZgaJpZM4QEA2t .

ncannasse commented 6 years ago

I have added make install that install both libs and headers on the system. ATM is uses/usr/lib and /usr/include because Ubuntu does not have /usr/local by default (I think). Maybe we should use /opt/local for OSX ? ping @andyli

andyli commented 6 years ago

/usr/local is the right prefix to use. Common practice is to create it if it doesn't exist, and it's what the haxe Makefile does: https://github.com/HaxeFoundation/haxe/blob/3.4.4/Makefile#L125