Coder Social home page Coder Social logo

Linking errors with nwindowGetDefault() about libnx HOT 8 CLOSED

 avatar commented on June 6, 2024
Linking errors with nwindowGetDefault()

from libnx.

Comments (8)

fincs avatar fincs commented on June 6, 2024

First of all, can you post your CMakeLists.txt? libnx is already linked in automatically as a system lib, you don't need to manually ask for it. During our testing, we found it sufficed to use this to link in deko3d:

target_link_libraries (${PROJECT_NAME} PRIVATE
	deko3dd
)

from libnx.

 avatar commented on June 6, 2024

@fincs I managed to reproduce the issue with a basic sample. It turns out nwindowGetDefault() cannot be called from C++, only from C. I've attached a reproduction to this comment. If you rename main.cpp to main.c and update the CMakeLists.txt to reference it, the build will succeed, and by default it will fail.

For reference I generate a Makefile with the following command:

cmake -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=../cmake/DevkitA64Libnx.cmake ..

sample.zip

from libnx.

 avatar commented on June 6, 2024

Here's the full build output demonstrating the issue:

Scanning dependencies of target sample
[ 25%] Building CXX object CMakeFiles/sample.dir/main.cpp.obj
/Users/lyptt/Desktop/sample/main.cpp: In function 'int main()':
/Users/lyptt/Desktop/sample/main.cpp:4:12: warning: variable 'a' set but not used [-Wunused-but-set-variable]
    4 |   NWindow* a = nwindowGetDefault();
      |            ^
[ 50%] Linking CXX executable sample
/opt/devkitpro/devkitA64/bin/../lib/gcc/aarch64-none-elf/11.1.0/../../../../aarch64-none-elf/bin/ld: /opt/devkitpro/devkitA64/bin/../lib/gcc/aarch64-none-elf/11.1.0/../../../../aarch64-none-elf/bin/ld: DWARF error: can't find .debug_ranges section.
CMakeFiles/sample.dir/main.cpp.obj: in function `main':
main.cpp:(.text.startup.main+0x8): undefined reference to `nwindowGetDefault()'
collect2: error: ld returned 1 exit status
make[2]: *** [sample] Error 1
make[1]: *** [CMakeFiles/sample.dir/all] Error 2
make: *** [all] Error 2

from libnx.

fincs avatar fincs commented on June 6, 2024

As I suspected, you're using third party/unsupported cmake machinery. You might be interested to know that, as of recently, we've added official cmake toolchain support. You can get it by installing or refreshing your switch-dev package group installation, i.e. sudo dkp-pacman -S --needed switch-dev.

In order to use it, we offer a wrapper script that sets the correct variables: /opt/devkitpro/portlibs/switch/bin/aarch64-none-elf-cmake. It is advised to purge your project of traces of previous unsupported machinery, and move to our new official support.

Setting up a basic project with deko3d should be as simple as this:

cmake_minimum_required (VERSION 3.7)
project (MyProjectName)

add_executable (${PROJECT_NAME}
	# Source code files go here
	source/main.cpp
)

dkp_add_asset_target (${PROJECT_NAME}_romfs romfs)

nx_generate_nacp (${PROJECT_NAME}.nacp
	NAME "Your Project Name"
	AUTHOR "Author Name"
)

nx_create_nro (${PROJECT_NAME}
	NACP ${PROJECT_NAME}.nacp
	ROMFS ${PROJECT_NAME}_romfs
)

target_compile_options (${PROJECT_NAME} PRIVATE
	-g -Wall -O2
	$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti>
)

target_link_libraries (${PROJECT_NAME} PRIVATE
	deko3dd
)

# Shaders
nx_add_shader_program (simple_vsh simple_vsh.glsl vert)
nx_add_shader_program (simple_fsh simple_fsh.glsl frag)

dkp_install_assets (${PROJECT_NAME}_romfs
	DESTINATION shaders
	TARGETS
		simple_vsh
		simple_fsh
		# etc
)

from libnx.

 avatar commented on June 6, 2024

@fincs I've confirmed that C++ name mangling is the issue here. If it's intended that this API has C linkage, you'll need to wrap the header definitions in an extern "C" block. Doing this manually myself on my local copy of native_window.h fixed the linking issue.

I've attached the modified file so you can see the change.

native_window.h.zip

from libnx.

 avatar commented on June 6, 2024

Thanks for a sample of the supported approach, I couldn't find any examples yesterday of using libnx with cmake when I started playing around with it, so I had to go my own route.

from libnx.

fincs avatar fincs commented on June 6, 2024

Oh and one more thing. The header you need to include is #include <switch.h>, which does have C++ guards. Trying to manually include libnx headers piecemeal is currently not supported.

from libnx.

 avatar commented on June 6, 2024

Gotcha, I didn't see any documentation relating to that quirk of the toolchain either. I'll bear that in mind from now on. Thanks for the help.

from libnx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.