-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Absolute imports don't work in C++ #794
Comments
@kjczarne Thanks for reporting the bug! I found out that it has been demonstrated by test I fixed it in commits kaitai-io/kaitai_struct_compiler@07d71cc...f725faf and the |
Awesome, thank you! 😊 |
This code used to call the `importFile` method since its introduction is very naive (and thus incorrect): ```scala curClass.meta.imports.foreach(file => lang.importFile(file)) ``` It takes the full absolute/relative path found in `meta/imports` and generates an import statement for it. So in practice, this works as intended only if none of the `meta/imports` paths contain any slashes (i.e. for relative .ksy imports within the same directory). KSC currently generates all output files to the same directory, so any non-flat import resembling a relative/absolute path is incorrect. This became clear already in the C++ implementation of imports in KS 0.8 when it also used `importFile` - kaitai-io/kaitai_struct#794 reported that "absolute paths don't work", because they looked like this, which obviously doesn't work: ```cpp #include "/network/ipv4_packet.h" // <-- no folder "network" #include "/network/ipv6_packet.h ``` The solution was to stop using `importFile`. But even if the `meta/import` path has no slashes, it's still not guaranteed to work. Names of output files that KSC generates are derived from `/meta/id`, but `meta/import` is a list of .ksy file paths, and `/meta/id` and the .ksy file name are generally not required to match. If they don't, the generated code will be broken in languages using `importFile` (which is only Nim at the moment), for example: main.ksy ```ksy meta: id: main imports: - imported-spec seq: - id: foo type: imported ``` imported-spec.ksy ```ksy meta: id: imported ``` ```console $ kaitai-struct-compiler -- --verbose file -d compiled/nim -t nim main.ksy parsing main.ksy... reading main.ksy... reading ./imported-spec.ksy... ... compiling it for nim... .... writing main.nim .... writing imported.nim ``` This is the beginning of the generated `main.nim`: ```nim import kaitai_struct_nim_runtime import options import imported-spec import "imported" ``` It will not compile due to invalid `import imported-spec` (since `imported-spec.nim` doesn't exist). But note that there is already correct `import "imported"` in place, so removing `import imported-spec` will actually fix things.
Hi, I've been trying to compile to cpp_stl and I've encountered something weird. I am not entirely sure if I am doing something wrong or whether this is a bug, so please excuse me if this seems like a newbie question ;)
I get warnings from IntelliSense that the
#include "/network/ipv4_packet.h"
and#include "/network/ipv6_packet.h"
cannot be found:The compiler dumps all files into a common folder without creating a
network
subfolder. Compiler command used was:ksc C:\Users\kczarnecki\Desktop\kissy\kissy\src\formats\network\pcap.ksy -t cpp_stl --cpp-namespace kaitai --cpp-standard 11
Interestingly when I manually patch every
#include
directive to use header files from the same folder and compile the following code:I get a validation error, stemming from a seemingly incorrect magic number:
I've set the KS_STR_ENCODING_NONE.
Parsing the very same PCAP with a Python build works perfectly. I'm using a kaitai struct compiler on Windows (commit 031c074) and the freshest cpp runtime library version.
The text was updated successfully, but these errors were encountered: