"undefined reference to `vkCreateInstance#12'" error when compiling C++ using Vulkan - c++

I am trying to learn C++ and Vulkan while using windows 10. I have created a small program that I am compling with minGW. However when I add vkCreateInstance(&instanceinfo, nullptr, &myvulkaninstance) i get an error:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\%USERNAME%\AppData\Local\Temp\ccDoOvlg.o:Renderer.cpp:(.text+0x5b): undefined reference to 'vkCreateInstance#12'
collect2.exe: error: ld returned 1 exit status
I get the above error in the 'Renderer.cpp' in the 'InitializeInstance' method (code can be seen below).
To compile my project I use this line in CMD g++ -L "C:/VulkanSDK/1.2.154.1/Lib/vulkan-1.lib" main.cpp Renderer.h Renderer.cpp -o build.exe. I do not know if I am compiling the library for Vulkan correctly as I was unable to find any good documentation about that online.
main.cpp
#include <iostream>
#include "Renderer.h"
using namespace std;
int main() {
Renderer renderer;
return 0;
}
Renderer.cpp
#include <iostream>
#include <cstdlib>
#include "Renderer.h"
// I again don't know if this is the right way to include this header
#include "C:/VulkanSDK/1.2.154.1/Include/vulkan/vulkan.h"
using namespace std;
Renderer::Renderer(){
}
void Renderer::InitializeInstance(){
VkInstanceCreateInfo instanceCreateInfo {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
// This is the line where i get the error
auto error = vkCreateInstance(&instanceCreateInfo, nullptr, &vulkanInstance); // <--
if(VK_SUCCESS != error){
std::exit(-1);
}
}
Renderer::~Renderer(){
}
I have been using OpenGL and java (using LWJGL) for a while but now I wanted to learn Vulkan and C++. I am however still learning so I don't know a lot about any of these subjects. If you know how I could improve my compiling and/or prevent this error, please let me know.

Is your operating system 64-bit? If it is, please download MSYS2 and uninstall 32-bit MinGW. In MSYS2, use clang64 toolchain to compile your code again. It's not recommended to use 32-bit compilers with Vulkan SDK.

Related

Trying to use Boost library on Codeblocks gives an undefined reference

I'm trying to use the boost library on CodeBlocks, but I'm new to it and I can't seem to be able to link it properly.
The boost folder(version 1.70) is in the same folder of my main.cpp, and the library I'm trying to access is libboost_filesystem-mgw92-mt-x64-1_70.a;
Here is my code:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path l_path("C:\\Hello.txt");
if(boost::filesystem::exists(l_path))
{
std::cout<<"exists!"<<std::endl;
}
else
{
std::cout<<"no";
}
return 0;
}
And some screenshots of my settings and of the error
Thank you!
Undefined reference to _Unwind_Resume suggests that you build Boost with different compiler than your project or you choose different type of exception handling.
Check if you're using the same compiler in both cases.
It might be also caused by building your project using gcc instead of g++. You should check that as well. In this case switch to g++ or explicitly link against libstdc++, by adding -lstdc++ to compiler flags.

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

How to include SDL2 and SDL_image on both Windows and Linux

I've been developing using C++ and SDL2 on Linux and have been using the following form to include SDL2 in my headers :
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
However, I now have need to develop on Windows too, which has a different include declaration. I've read up on it as best I can and have successfully included SDL using #include "SDL.h" and passing -I/usr/include/SDL2 into my makefile. This works fine for SDL, but seems to break SDL_image.
Using :
#include "SDL.h"
#include "SDL_image.h"
results in a long list of errors starting with
undefined reference to `IMG_Load'
Using
#include "SDL.h"
#include <SDL2/SDL_image.h>
with the -lSDL2_image flag results in
undefined reference to symbol 'SDL_FreeSurface'
Both of these errors I know are to do with SDL_image. The only thing that seems to work is
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
with the -lSDL2 -lSDL2_image when compiling, but I'd rather avoid that if possible so that I don't have to use #ifdef to compile on both Linux and Windows.
Can anyone point me in the right direction as to where I'm going wrong please?

Beginner working with objects and classes getting the following errors

This is a tutorial I've been following and I've done everything that it tells but it doesn't work. I have three files: the main.cpp, burrito.h (the class), and burrito.cpp.
And here are the three files respectively.
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main() {
Burrito bo;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito {
public:
Burrito();
};
#endif // BURRITO_H
Burrito.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
Burrito::Burrito() {
cout << "Hello World" << endl;
}
When I build and run I get the following error:
...undefined reference to `Burrito::Burrito()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 6 seconds)
1 errors, 0 warnings
I'm compiling using CodeBlocks.
I'm using CodeBlocks
This is the issue.
If you’re starting to learn C++ it’s (unfortunately) essential to learn about translation units. An IDE like Code::Blocks hides this detail from you – and does it wrong, in this case (this isn’t really the fault of Code::Blocks though, it can’t automatically guess what to do in this case without configuation).
In the beginning, drop the IDE, go to the command line for compiling. Compile the two translation units separately and link them together explicitly.
g++ -o burrito.o burrito.cpp
g++ -o main.o main.cpp
g++ -o main main.o burrito.o
Every good beginners’ C++ book will explain how this works.
When you're linking objects together to get the final executable file you're forgetting to link the compiled-object from burrito.cpp file correctly
If you're building with a Makefile, your final output rule should have something like "-o main main.o burrito.o"
Using Code Blocks 13.12 I right clicked on the Burritto.cpp file chose Properties, then chose the Build tab and checked the compile file and link file check boxes then clicked ok saved everything then ran and it worked great.

Suspending system using c++ program

I am trying to suspend my system using a c++ program using SetSuspendState method but I am facing issue during linking.
I am using g++-4 (GCC) 4.3.4 20090804 (release) 1 compiler on Windows 7 OS (64bit).
The code I have written is
#include <iostream>
#include "windows.h"
#include "powrprof.h"
using namespace std;
int main() {
cout << SetSuspendState(false, true, false);
return 0;
}
Following is the error I am facing:
/cygdrive/c/Users/Vikas/AppData/Local/Temp/ccFpLgPi.o:suspend.cpp:(.text+0xa4):
undefined reference to
`_SetSuspendState#12' collect2: ld
returned 1 exit status
Kindly help me in resolving this issue.
Thanks in advance ...
I believe dlltool can be used to create import libraries from DLLs for use with GCC under Cygwin. The DLL exporting the functions pwrprof.dll should be located in your Windows system directory somewhere.
As msdn says you need to link PowrProf.lib.