Nuklear icon indicating copy to clipboard operation
Nuklear copied to clipboard

Cannot compile sample project with nuklear

Open SimonRegistrierung opened this issue 3 years ago • 4 comments

Hi there,

ich have an annoying issue with compiling the sample project, which I canot get rid of. My project structure looks like this:

grafik with the nuklear directory being the freshly downloaded and extracted copy of the nuklear repository.

the CMakeLists.txt is:

cmake_minimum_required(VERSION 3.20)
project(nuklearTest)

set(CMAKE_CXX_STANDARD 14)

add_executable(nuklearTest main.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/nuklear)

here's my main.cpp:

#include <iostream>
#include "nuklear.h"

int main()
{
	/* init gui state */
	struct nk_context ctx;
	nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);

	enum {EASY, HARD};
	static int op = EASY;
	static float value = 0.6f;
	static int i =  20;

	if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
		NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
		/* fixed widget pixel width */
		nk_layout_row_static(&ctx, 30, 80, 1);
		if (nk_button_label(&ctx, "button")) {
			/* event handling */
		}

		/* fixed widget window ratio width */
		nk_layout_row_dynamic(&ctx, 30, 2);
		if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
		if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;

		/* custom widget pixel width */
		nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
		{
			nk_layout_row_push(&ctx, 50);
			nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
			nk_layout_row_push(&ctx, 110);
			nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
		}
		nk_layout_row_end(&ctx);
	}
	nk_end(&ctx);

}

First, I am missing references to the constant MAX_MEMORY and &font. I have manually created them at the beginning of the main.cpp function like this (I don't know if that's correct, but at least the CLion IDE doesn't complain anymore):

const int MAX_MEMORY = 128;
nk_user_font font = (const nk_user_font&)"Times New Roman";

but now I encounter the error:

main.cpp.obj : error LNK2019: unresolved external symbol nk_begin referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_end referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_layout_row_dynamic referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_layout_row_static referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_layout_row_begin referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_layout_row_push referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_layout_row_end referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_label referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_button_label referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_option_label referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_slider_float referenced in function main
main.cpp.obj : error LNK2019: unresolved external symbol nk_rect referenced in function main
nuklearTest.exe : fatal error LNK1120: 13 unresolved externals

I am quite sure the nuklear.h is correctly linked, as I can implement other functions from within the IDE. I already played around with other CMake commands, like include_directories in order to find nuklear, but to no avail. I have also tried to include:

#define NK_IMPLEMENTATION
#include "nuklear.h"

Then, the project compiles but I get this error during execution:

grafik

There must be something I am missing. Any help is welcome. Thanks

SimonRegistrierung avatar Aug 18 '21 08:08 SimonRegistrierung

You need to define NK_IMPLEMENTATION before including nuklear.h (header files are never compiled). Check the demo to understand the concept of single header libraries.

riri avatar Aug 21 '21 09:08 riri

We might want to fix the example in the README so that it does not reference undefined font and newcomers stop bumping into this.

lockie avatar Oct 06 '21 19:10 lockie

@SimonRegistrierung were you able to resolve your issue?

I am having difficulty with finding how to define nk_user_font.

I tried the same nk_user_font font = (const nk_user_font&)"Times New Roman";. I am able to compile an executable but am getting Segmentation Fault at runtime. My main function is calling another function that uses SDL though. I am not sure if the problem is the font object or not integrating with SDL. Did that font line work for you?

persinammon avatar Nov 28 '23 01:11 persinammon

@persinammon : nk_user_font isn't a string on which you define the name of a system font. Please read the demos source code and nuklear.h comments (part dealing with font baking) to understand those concepts.

Nuklear isn't a ready to use system, and from @SimonRegistrierung example I already see it's not a full program, there is no implementation backend, so nothing will happen.

I don't think the README example is a problem, it's a snippet (to me it seems clear it's not a complete program). Maybe a hint to refer to demos directory to see how the link with underlaying system is done would be useful tho.

riri avatar Nov 28 '23 10:11 riri