I am trying to add https://github.com/mlabbe/nativefiledialog this C library to my C project in visual studio 2022. I've built and generated the .lib file and added it to my project by going to Project->Properties->Linker. I added the path to this .lib file to Additional Library Directories. After doing this, I am still receiving "unresolved external symbol" linker errors. Specifically LNK2019 and LNK1120. I've also added the header file to the Additional Include Directories. What other steps am I missing?
Thanks for the help in advance!
EDIT:
Here is the code implementation:
StartupLayer.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Walnut/Application.h"
#include "ExcelReader.h"
#include "nfd.h"
#define BUFFER_SIZE 255
class StartupLayer : public Walnut::Layer {
private:
ExcelReader reader;
char inputPath[BUFFER_SIZE] = "enter folder";
public:
StartupLayer() : reader(ExcelReader()) {}
~StartupLayer() {}
virtual void OnUIRender() override;
};
StartupLayer.cpp
#include "StartupLayer.h"
void StartupLayer::OnUIRender() {
ImGui::Begin("Select Folder");
ImGui::InputText("select folder", inputPath, BUFFER_SIZE);
// when browse button is clicked
if (ImGui::Button("Browse")) {
nfdchar_t* outpath = NULL;
nfdresult_t result = NFD_OpenDialog(NULL, NULL, &outpath);
}
ImGui::End();
}
It's a Dear ImGui project, and I wanted to use this open file dialog library in one of my layers. The above code is that layer class, and I'm getting the unresolved external symbol on the NFD_OpenDialog function call.
CodePudding user response:
I suggest you read this documentation carefully and add #pragma comment(lib,".lib")
after #include""
.
CodePudding user response:
C Code
The C app contains just a little function:
// ./code/c/lib.c
#include <stdio.h>
__declspec(dllexport) void f()
{
printf("\n This is a C code\n");
}
The keyword __declspec(dllexport)
is only valid within the Microsoft compiler world and exports that function.
Compile C
Open the Visual Studio Developer Command Prompt and navigate to the folder, where the C project is. Then simply type: cl /LD lib.c
. This will create two files. A lib.lib
and a lib.dll
.
C Code
Also nothing special. First you need the header file:
// ./code/cpp/Console/Console/Header.h
#pragma once
extern "C" {
__declspec(dllimport) void f();
}
You find declspec
again, but this time it is specified with dllimport
which makes perfectly sense as you are importing the function
Our app:
// ./code/cpp/Console/Console/Console.cpp
#include <iostream>
#include "Header.h"
#pragma comment(lib, "../../../c/lib.lib")
int main()
{
f();
}
Note the line #pragma comment(lib, "../../../c/lib.lib")
which specifies the location of the .lib
so the linker is able to reference the function f()
.
Execute
If you now execute Console.exe
you will get an error because the dll can not be find. To fix this, just copy the lib.dll
to the folder where Console.exe
is.
CodePudding user response:
I just forgot to add the path to the generated .lib file (nfd.lib) to Projects->Properties->Linker->Input Additional Dependencies. After doing this, everything worked perfectly.