Error: Multiple targets

Beginner here, just downloaded haxe and vscode and starting on hello world projects
and getting variants of this

other errors include “you cannot access the cpp package while targeting cross”
i am out of ideas and any insight is much appreciated

Both error points to your build.hxml, with either too much or not enough target selected (you should have only one).

Can you show us build.hxml?

More information on it HXML - Haxe - The Cross-platform Toolkit

Unrelated, but considering the indentation in that screenshot, you may want to have a look at Auto Indentation · vshaxe/vshaxe Wiki · GitHub.

1 Like

here you go,figured it was something wrong with the build file but still no clue how to fix it

What about .vscode/launch.json (and .vscode/tasks.json if it exists)?

launch
.vscode/launch.json

and
tasks

.vscode/tasks.json

new users are capped at 1 img per post

Hm, not sure where the second target comes from then… or do you perhaps also have a settings.json with haxe.configurations?

ah, the build.hxml wasn’t saved, this is the actual one
actual build

Well, yes, that contains multiple targets as the error states (neko and interp) - that’s not going to work.

yeah figured as much, any advice ? or at least point me to the right direction?

Advice for what exactly? I’m not sure what you’re trying to do…

well… a hello world code that ended in a dumpster fire. the code was from a haxe guide book

however nothing is mentioned about the build.hxml file and it apparently is too basic for any tutorials i found to cover
either way i just found some docs on github on the issue
thanks for ur help :smiley:

A book intended for Haxe 2 is perhaps not the best starting point in 2020 anyway (when we’re at Haxe version 4)… :wink:

The resources on haxe.org itself should be fine for getting started:

Hi, gccc.

You need to make one hxml file for each target you wish to compile to.

Possibly also useful to you, I’ve put up my own tutorial at http://www.unexpected-vortices.com/haxe/brief-tutorial.html, but I’m on GNU/Linux, and, also, am not using vscode.

thanks man, will look into it

To go into more detail about your errors and how to use haxe:

The build.hxml file is just a list of arguments passed to the haxe command

If you click the terminal tab at the bottom in vscode and enter
haxe --help

It will show all the the arguments you can pass in. Here’s what you get:

Haxe Compiler 4.1.0-rc.1 - (C)2005-2019 Haxe Foundation
Usage: haxe <target> [options] [hxml files...]

Target:
  --js <file>                           compile code to JavaScript file
  --lua <file>                          compile code to Lua file
  --swf <file>                          compile code to Flash SWF file
  --neko <file>                         compile code to Neko Binary
  --php <directory>                     generate PHP code into target directory
  --cpp <directory>                     generate C++ code into target directory
  --cppia <file>                        generate Cppia code into target file
  --cs <directory>                      generate C# code into target directory
  --java <directory>                    generate Java code into target directory
  --python <file>                       generate Python code as target file
  --hl <file>                           compile HL code as target file
  --interp                              interpret the program using internal macro system

Compilation:
  -p, --class-path <path>               add a directory to find source files
  -m, --main <class>                    select startup class
  -L, --library <name[:ver]>            use a haxelib library
  -D, --define <var[=value]>            define a conditional compilation flag
  -r, --resource <file>[@name]          add a named resource file
  --cmd <command>                       run the specified command after successful compilation
  --remap <package:target>              remap a package to another one
  --macro <macro>                       call the given macro before typing anything else
  -C, --cwd <dir>                       set current working directory
  --haxelib-global                      pass --global argument to haxelib

(full details here Compiler Usage - Haxe - The Cross-platform Toolkit)

‘Targets’ represent the different languages and bytecode output haxe can generate. It can only generate one at a time, so you can only have one target argument when you run haxe. (Although it’s possible to have multiple targets in a single hxml file if you use --next)

So a hello world that runs in js, python and haxe’s internal runtime:

Main.hx

class Main {
    static function main() {
        trace('hello world!');
    }
}

build-js.hxml

--main Main
--js main.js

^ This one will generate a file called main.js which you could run in a browser or with node.js (node main.js)

build-python.hxml

--main Main
--python main.py

^ This one will generate a file called main.py which you could run with python if it’s installed (python main.py)

run-interp.hxml

--main Main
--interp

^ Rather than generating a file, this one will execute the code in haxe’s internal runtime, so this will just print “hello world!” when you execute it

To execute those hxml files you can enter haxe build-js.hxml into the terminal tab of vscode or you can run the build command in vscode. From the “Terminal” menu, click “Run build task” and select the hxml you want to build there.

You can add --dce full to all of those hxml files to enable dead-code-elimination which will make the output files smaller

The neko target was a runtime that was included with haxe. It is generally not used much any more and has been superseded by the --interp target and the --hl target.

The error about “You cannot access the cpp package while targeting cross” happens when you try to use packages from the standard library that start with a target name (like js.Object or neko.sys.arg). This code is target-specific code and can only be used when compiling for that target. So you can use anything in js. only if you have --js in your build.hxml.

In your example you use the command-line arguments for the name. Here’s how you might do that

Main.hx

class Main {
    static function main() {
        var name = Sys.args()[0];
        if (name != null) {
            trace('hello ${name}');
        } else {
            trace('no name');
        }
    }
}

Now for the target, let’s pick --interp because it’s simple. We can run by entering this into the terminal
haxe --run Main.hx MyName, which runs the code via the interp target.

To use neko you can do
haxe --main Main --neko main.n
Then execute the generated .n file with neko
neko main.n MyName.

I said neko has been superseded by --hl (hashlink), but a hashlink executable isn’t yet bundled with haxe whereas neko is, so it will soon be superseded.

If you want something that can be run standalone then use the --cpp target. This might require some setup to compile the C++ code, I’ve not tried this on windows for a while but it probably requires installing visual studio which is a pain.

Good luck! Let me know if you have any more questions!

5 Likes