2

I am developing a module, which depends on another library(wasmtime). I put files into:

  • modules/mod_wasm/src/include - header files, and
  • modules/mod_mine/src/lib/libwasmtime.a - the compiled library.

The problem which I faced is that when I compile the acore server with

./acore.sh compiler all

it gives me the error:

[100%] Linking CXX executable worldserver
/usr/bin/ld: ../../../modules/libmodules.a(ModWasm.cpp.o): in function `readWasmFile(char const*)':
ModWasm.cpp:(.text+0x63): undefined reference to `wasm_byte_vec_new_uninitialized'
/usr/bin/ld: ModWasm.cpp:(.text+0xce): undefined reference to `wasm_byte_vec_delete'

The question is it required somehow add to a config that library? If yes, then how to do that?

I was testing my code in simple main.cpp file and it was working with options like "-L${workspaceFolder}/lib" and "-lwasmtime". Maybe, these options are also required for my module?

Here is a link to azerothcore project which I use. my module locates in modules/mod-wasm folder

azerothcore-wotlk/modules ‹master*› » tree -L 3 mod-wasm
mod-wasm
├── CMakeLists.txt
├── LICENSE
├── Makefile
├── README.md
├── conf
│   ├── conf.sh.dist
│   └── wasm.conf.dist
├── include.sh
├── mod-wasm.cmake
├── setup_git_commit_template.sh
├── src
│   ├── ModWasm.cpp
│   ├── include
│   │   ├── doc-wasm.h
│   │   ├── wasi.h
│   │   ├── wasm.h
│   │   ├── wasmtime
│   │   ├── wasmtime.h
│   │   └── wasmtime.hh
│   ├── lib
│   │   ├── libwasmtime.a
│   │   └── libwasmtime.so
│   └── wasm_loader.cpp
└── wasm_modules
    └── rust_wasm_app.wasm

As I understood from the logs what I see and because CMakeList.txt exists in modules folder, the project considers the folder as module. Which in its turn scans subdirs for *.cmake files and configures the project.

The question now is how to properly configure my module to show that it contains the compiled library wasmtime inside src/lib folder? As I understood, I could use target_link_libraries, but it requires a target name, and I have no idea what it should be and where I can take it.

Alex Ly
  • 139
  • 5
  • What did you specify as linked libraries in `target_link_libraries(worldserver ...)`? Please this info for any linked cmake targets of your own creation that are linked directly or indirectly to `worldserver` too... – fabian Oct 18 '22 at 17:26
  • Alternatively run `cmake --build` with the `--verbose` option and post the linker command. – fabian Oct 18 '22 at 17:28
  • @fabian can you elaborate, why you believe that's a cmake problem? – πάντα ῥεῖ Oct 18 '22 at 17:38
  • 1
    @πάνταῥεῖ This may not be a cmake problem but the output matches the output of building a cmake project configured with the "Unix Makefile" generator of CMake and with the info I requested we'd at least know more about linker options. – fabian Oct 18 '22 at 17:44
  • @fabian, i add nothing. I mean, I used script to create an azerothcore module from skeleton. Then add my script into it. Then tried to compile. I am sorry, but all this target link and so on is foreign language for me :). As I can understand I can add it in cmake config, which, as i so in code, can be included in a module. Could you describe it a little bit more? – Alex Ly Oct 18 '22 at 17:45
  • @AlexLy If my assumption is correct, CMake is used somewhere in your build process. The whole process usually goes like this: 1. CMake configuration (Usually something like `cmake ... -S ... -B ...` or `cmake ... some/path` 2. building the generated project (`cmake --build ...` or `make`). In this second step you should be able to activate verbose output which should result in the linker command being printed; this kind of info would allow us to check, what you're actually linking. – fabian Oct 18 '22 at 17:51
  • As for `target_link_libraries` not sure if collecting this info is worth while, if you just copy&pasted something from a tutorial and did some adjustments. Basically `target_link_libraries` establishes a relationship between the linking target and libraries or cmake targets and there is the possibility of the transitive inheritance (e.g. linking cmake lib target `X` that links lib `Y` to target `A` could result in both `X` and `Y` to be linked to `A` when linking). – fabian Oct 18 '22 at 17:56
  • @fabian, thank you for the clarification. Yes, under the hood it uses CMake. The problem is that it is "highly configured" and separated in many sh scripts which makes it difficult to find exact command. Anyway, i will try and when I find it, i'll post the result. – Alex Ly Oct 18 '22 at 18:01
  • hi, @fabian. after spending my evening to understand how the project is configured I can surely say, that it uses `cmake` to configure, but it uses `make` to compile. I think i will adjust description of my question explaining project structure and what I am missing. Because, at current point of undestanding c++ and cmake, i see that it is clearly missing linkin (park? :) ) configuration inside my module, but I do not know how properly make it, and examples do not help much, because I do not know for example what app_name should be used to link and so on... – Alex Ly Oct 19 '22 at 07:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248897/discussion-between-alex-ly-and-fabian). – Alex Ly Oct 19 '22 at 08:20
  • @πάνταῥεῖ closing this question and marking it as duplicate is not helpful as the other question you linked is too generic and clearly does not help the user to solve their specific problem. Please consider reopening the question. – Francesco Borzi Oct 19 '22 at 09:41
  • @FrancescoBorzi: If you think that the question should not be closed, then why don't to vote for reopen it? – Tsyvarev Oct 19 '22 at 15:51
  • 1
    Well, the question is NOT about meaning of the error message, which is described in the generic "What is undefined reference" question. The question is about **how to link** AzerothCore modules. And while the AzerothCore project is built using CMake, its modules are described using some another "language". So even placing a `target_link_libraries` call could be a problem. Reopened. – Tsyvarev Oct 19 '22 at 16:03
  • @Tsyvarev thanks, I did not see that option. I only saw the option to edit and request reopening, but I did not want to make any edits – Francesco Borzi Oct 19 '22 at 16:17
  • @Tsyvarev, ok. done it. but i did not have `tree`, so I had to install it. – Alex Ly Oct 19 '22 at 20:50

1 Answers1

2

At the end, I was able to find an answer with try and catch. Azerothcore modules supports modname.cmake file to be run when configure libmodules.a which contains all extra modules(if I understood it correctly. this is part of modules/CMakeFiles.txt

# Enables Devs to Include a cmake file in their module that will get run inline with the config.
foreach(SOURCE_MODULE ${MODULES_MODULE_LIST})
message("SOURCE_MODULE: ${SOURCE_MODULE}")
    include("${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE}/${SOURCE_MODULE}.cmake" OPTIONAL)
endforeach()

here I have my dirty cmake file which allow me to compile the server

set(WASM_MODULE_DIR ${CMAKE_SOURCE_DIR}/modules/${SOURCE_MODULE})
set(WASM_MODULE_SRC_DIR ${WASM_MODULE_DIR}/src)

message("--------------------->>>>> APPLICATION_NAME : ${APPLICATION_NAME}")
message("--------------------->>>>> APP_PROJECT_NAME : ${APP_PROJECT_NAME}")
message("--------------------->>>>> SOURCE_MODULE : ${SOURCE_MODULE}")
message("--------------------->>>>> WASM_MODULE_DIR : ${WASM_MODULE_DIR}")
message("--------------------->>>>> WASM_MODULE_SRC_DIR : ${WASM_MODULE_SRC_DIR}")
# include wasmtime
target_include_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/include)
target_link_directories(modules PUBLIC ${WASM_MODULE_SRC_DIR}/lib)

find_library(LIBWASMTIME_TO_INCLUDE NAMES wasmtime PATHS ${WASM_MODULE_SRC_DIR}/lib REQUIRED)
message("--------------------->>>>>>>>> LIBWASMTIME_TO_INCLUDE: ${LIBWASMTIME_TO_INCLUDE}")
target_link_libraries(modules PUBLIC wasmtime)

So, it compiles now. But I have next problem, which I am trying to resolve. but this is another story.

Thank you all for the help

Alex Ly
  • 139
  • 5