can't seem to link GLFW library - c++

I'm attempting to make my own game engine and using this video tutorial to do so: https://www.youtube.com/watch?v=Pid8JGlBdPY&t=1122s&ab_channel=TheCherno
I'm using library called GLFW. I linked everything as its shown in the video triple checked everything but every time I use function from the library I get errors.
this is all the code I've written
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main()
{
if (!glfwInit()) {
cout << "NOPE" << endl;
}
return 0;
}
I'm guessing I've linked something wrong even though I can't find what. but if that's not the issue here and im missing something, please let me know. I don't really know what those errors mean.

Related

Console doesn't show output from vector

I have a weird problem when trying to print out the content of a vector.
I'm using Visual Studio Code with the CMake extension.
I can print out simple text using cout
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "test" << endl;
return 0;
}
But I can't print out the vectors content
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> test = {1,2,3};
cout << "test" << endl;
cout << test[1] << endl;
return 0;
}
I've never really worked with C++ vectors, so I'm probably missing something fairly obvious but I followed a C++ vector tutorial step by step and for them, the output works fine.
Cheers,
Luca
I'm new to C++ and have exactly the same problem.
Temporary workaround as posted by Luckylone here
would be to copy libstdc++-6.dll from your mingw64\bin folder into your project folder.
Something is messing up our link with the resources even though folders are properly added to system PATH.
EDIT: After an afternoon of troubleshooting, I've solved my problem by uninstalling compilers (originally provided through WinLibs) and reinstalling them using MSYS2.
Delete the existing Mingw64 folder, remove the PATH variables, then carefully follow these instructions. Keep in mind that I had to add mingw64/bin to both user and system Path, and restart VS Code before damn std::vector finally started to print.

getline(ifstream, string) on Mac causes EXC_BAD_ACCESS

I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.

Delay in CodeWarrior

I am new to trying to write anything. While I can read what's happening most of the time, I have no idea how to build a delay. In Arduino I have used delays but it doesn't seem to work the same here.
I have been searching the internet trying to find something that will work, but with no luck. I think I could make something work but I don't know how to add more '#includes' either. Currently I have-
#include <xbee_config.h>
#include <types.h>
#include <utils.h>
#include <xbee/atcmd.h>
I have the general idea of what is needed but now idea how to write it. I'm turning on an LED that I need delayed before turning off.
gpio_set(LED1, 1); //Turn on LED
**Delay here!!!!**
gpio_set(LED1, 0); //Turn off LED
My first thought is building a void_delay function that will increment a counter until x time is reached, then return to the program. I know that's not the best way as it will be keeping the program from other tasks while counting, but it should work for my purpose. The problem, I have no idea how to write that.
In c++ you can use Sleep(milliseconds) you only have to include <windows.h>.
Example:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
cout << "Before delay" <<endl;
Sleep(5000);
cout << "After delay" <<endl;
return 0;
}

Compiler/linker error "undefined reference"

Hi I am just starting to learn C++. I bought this big C++ for Dummies book and have been going through it. Its been really interesting so far but now I am stuck. I have been googling this problem, but to no avail. I am using I am using codeblocks 10.05 with GNU GCC.
I keep getting an error that says:
In function 'main':
undefined reference to 'SafeCracker(int)'
The code isn't complicated. I am just new and am extremely frustrated. I don't want to skip over this part; I want to know what is going on.
Main:
#include <iostream>
#include "safestuff.h"
using namespace std;
int main()
{
cout << "Surprise, surprise!" << endl;
cout << "The combination is (once again)" << endl;
cout << SafeCracker(12) << endl;
return 0;
}
Function:
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "13-26-16";
}
Header:
using namespace std;
#ifndef SAFESTUFF_H_INCLUDED
#define SAFESTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // SAFESTUFF_H_INCLUDED
You are not compiling the second file you listed along with the first one. Try compiling directly with gcc to understand this.
assuming your files are named:
main.cpp
SafeCracker.cpp
safestuff.h
This is what you are doing
gcc main.cpp
While you should be doing this
gcc main.cpp SafeCracker.cpp
Also, SafeCracker.cpp should be including the header file as well, just for clarity. Any reasons why you have them separated?
On another note, from seeing Daniel Hu's answer, <iostream> is automatically including <string> for you. You should not depend on this functionality, and should instead include <string> in each file that uses strings.
(From comment below)
You're probably trying to build your main.cpp as a stand-alone file. This will leave SafeCracker.cpp uncompiled. What you need is create a project in Codeblocks and add all three files to it (both *.cpp files as well as the *.h file).
I think it's because you did not #include <string>
C++ has to import the string library to use strings or else everything is treated as char arrays.

How to link C++ source files with Code::Blocks

I'm doing something wrong, I know. I can't quite figure out how to
link two .cpp files together through a header file. The calling
method can't see the other source.
I'm using Code::Blocks as an IDE with MinGW.
Any help would be greatly appreciated. It would be even more
appreciated if you could show the fixed source, link in the reply to a
pastebin page with it.
/***********************************main.cpp***********************************/
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
printTest(); //can't see printTest, defined in test.cpp
return 0;
};
/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void printTest();
#endif // TEST_H_INCLUDED
/***********************************test.cpp***********************************/
#include "test.h"
void printTest()
{
cout << "Hello world!" << endl;
};
You might find this code blocks wiki helpful. It looks like Code blocks uses a managed build system so if you add the file to the project properly then it should know to compile it and link in the object file that results.
And just to be more explicit about some other comments, when you use "using namespace std;" the namespace is only brought into scope for the file where the using statement is located. That is why others are telling you to explicitly specify the std:: namespace. You could also bring all of the std namespace into scope in the test.cpp file. Many people consider this a bad habit to get into. It's generally better to bring into scope just what you need via
using std::cout;
using std::endl;
Finally, remember that std::endl adds a new line AND flushes the buffer, it's not a good replacement for a new line character in all cases.
In test.cpp replace cout << "Hello world!" << endl;
by std::cout << "Hello world!" << std::endl;
sanket answer’s seems incomplete to me.
You need to add #include <iostream> in your test.cpp so that the compiler knows what "cout" is.
As sanket stated, you should use std::cout and std::endl in test.cpp.