Placement of cpp code

When entering the code for this lecture I initially placed it in the projects directory I created for the hello.cpp. However when I came to run the code using debugger it failed to compile, with the output in the picture below. I had to move the cpp file to the raylib vs-code directory in order to get a compile and run.

I received the same error, but moving my file into that directory didnā€™t help. Even my installation of the workspace folder didnā€™t go as it did in the video. Does anyone have any insight on this error? Thanks!

FINAL EDIT:
I FOUND THE SOLUTION!

TLDR: You need to make your .cpp file in the same directory as the vscode-template stuff!

I think between videos, Stephen either renamed the folder after getting it from the git repository, or made a copy of it and renamed that. Not sure which he did, but he never shown that he did in the video. Took me awhile to realize when he was creating his hello.cpp and axe-game.cpp files that the file explorer had a ā€œ.vscodeā€ folder. Thatā€™s because it was in that same directory. Recreated axe-game.cpp after opening up the workspace, then debugged it. Worked like a charm. Screenshot follows of my file explorer:

Just as a test, I also started fresh, made a copy of the ā€œvscode-templateā€ folder, renamed it and re-coded axe-game.cpp in the copy to see if it would still compile. It compiled just fine. Iā€™d recommend keeping the template untouched and just make a new copy of it that is named something more relevant to the project.

Original post content follows for forum historical integrity:

Also getting this. Tried going back through the videos to figure out what was up, but canā€™t seem to find a solutionā€¦ Looking for answers as well.

EDIT 1: As a test, I opened the VSCode sample in the Raylib directory and tried to debug with the default build task supplied with the sample project. Or so I thoughtā€¦ First time was a fail, but then I realized I was trying to ā€œcompileā€ the build file instead of main. Went back to the sampleā€™s main.cpp and tried again. Worked flawlessly. Going to attempt with this exercise. Will post results.

EDIT 2: Nopeā€¦ Going to try somethingā€¦ Wacky. Maybe copy over the makefile and stuff from the Raylib example project and try thatā€¦

EDIT 3: Didnā€™t work eitherā€¦

3 Likes

Youā€™re the man, JohnnyThreeDee! Thank you for this solution because this put a serious damper on my course progress. I hope there arenā€™t anymore huge roadblocks going further, but collectively I think we can get past them. Thanks again!

1 Like

Moving the axe_game.cpp back to the template folder sort of worked, but I got a different error.

Looks like it isnā€™t finding the header or the raylib library. Hereā€™s my code:

#include ā€œraylib.hā€

int main()

{

InitWindow(640, 480, "Hello");

}

Any help?

1 Like

So, changing the name of the source file to axe_game.c let me compile and link. Hmph.

Looking for places in the makefile I need to change from .c to .cpp to compile as c++. Little lost, but still digging.

Larry

1 Like

Oddā€¦ If itā€™s compiling with a .c file, it should work with a .cpp file as wellā€¦ Silly question, but did you open the workspace in the template folder and add axe_game.cpp to the workspace before compiling?

Yep, I sure did. However, I found a line in the tasks.json file under windows | args that was:

ā€œOBJS=${fileBasenameNoExtension}.cā€

I changed it to:

ā€œOBJS=${fileBasenameNoExtension}.cppā€

and it compiled. Iā€™m not sure why it works for everyone else without modification, but not for me. Perhaps I screwed something else up while I was poking around. Anyway, for the moment, I can compile. (But I can no longer compile .c files LOL. Only .cpp). I think there must be a better solution!

1 Like

Hey, if it works, roll with it lol.

Thanks for adding to the solution!

I too have the exact same error when I try to build i.e Run Build Task (Ctrl+Shift+B) on Windows.

Starting buildā€¦
C:\raylib\mingw\bin\g++.exe -g D:\VidProjects\CPlusPlus_Projects\GameDev\Project\axe_game.cpp -o D:\VidProjects\CPlusPlus_Projects\GameDev\Project\axe_game.exe
C:\WINDOWS\TEMP\ccoVJGeB.o: In function main': D:/VidProjects/CPlusPlus_Projects/GameDev/Project/axe_game.cpp:13: undefined reference to InitWindowā€™
collect2.exe: error: ld returned 1 exit status

However, if I Start Debugging (F5), the program runs perfectly fine and the window is displayed.

Not sure what is causing this issue

Hi there, for those who are struggling to build outside of using Start Debugging (F5), can you give this a try? Just focus on step 4, every other step can be skipped as they should already be completed via the template or through lectures up to this point.

1 Like

This is not very helpful to me. Iā€™m considering using a different IDE altogether.
Edit: I tried using Dev-CPP and Iā€™m getting the same error.
Edit 2: Copying the raylib source files into the same folder as the axe_game.cpp file doesnā€™t work.

Hi Guys,

The confusion i think is here is because we like to create folders for each section but when you create the folders it needs the associated template in that folder.

I will be highlighting this to Stephen as well.

The process i used.

Create a new folder.
Open the Raylib template.
Copy the contents (Ignoring any main.cpp from previous projects if you save it there)
Paste the contents to the new folder.
Open Vs Code
Click File and open the new workspace and browse to the workspace file in your new project folder.

If you are having issues from the last section make sure you reboot your machine as it needs to make changes to the OS.

(Reiterated from JohnnyThreeDeeā€™s post as well as working through)

2 Likes

Hi, your solution worked here, thank you very much marc!!

1 Like

Apologies all.
My explanation of the setup was not very good. Iā€™ve re-recorded the Setup and Installation videos, as well as the setup videos for Axe Game, Dapper Dasher and the Classy Clash game as well. Iā€™ve show all the steps that will get your code compiling.

Also, the VS Code template is now different, so youā€™ll need to download it again (I download it as a .zip in the video now).

And thereā€™s a Raylib VS Code template for raylib projects and a VS Code template for plain C++ projects as of now.

The process is to download the .zip file, extract it, and simply open the workspace. You can do it by right-clicking on the main.code-workspace file and clicking Open with Code (if you enabled that feature when installing, which Iā€™ve added to the installation video) or, if you donā€™t have that feature, simply open VS Code and go to File ā†’ Open Workspace and find the projectā€™s main.code-workspace and open that.

You donā€™t need to Configure Default Build Task or anything. The .vscode and Makefile take care of everything. You can create a source file and start coding, or use the projects on the Gitlab page. Everything should compile just fine (compiling will create an .exe of the game) as well as Start Debugging and Run Without Debugging.

Again, Iā€™m very sorry for all of the frustration as we were ironing out these kinks.

6 Likes

you are using the wrong compiler it looks like you are using g++ instead of cpp
i had the same problem and changing the compiler fixed it right up for me

Thank you Stephen! You the man!

1 Like

Hi Stephen, please help me pleaseā€¦ :sob:
I made a game that i can close it by pressing X key or ESCAPE key, until this tutorial everything was good, but when i debug it and run it, I cant move the circle to left and right by pressing ā€œAā€ or ā€œDā€, nothing happen by pressing these keys. please show me the way.

It my code:

#include "raylib.h"

int main()
{
    // Window layout
    int width{350};
    int hight{200};
    InitWindow(width, hight, "Halim Shams");

    SetTargetFPS(60);
    while (WindowShouldClose() == false)
    {
        // Circle demations
        int circle_x{175};
        int circle_y{100};

        BeginDrawing();
        ClearBackground(WHITE);
        DrawCircle(circle_x, circle_y, 25, BLUE);

        // Game starts

        if (IsKeyDown(KEY_A))
        {
            circle_x = circle_x - 10;
        }

        if (IsKeyDown(KEY_D))
        {
            circle_x = circle_x + 10;
        }
        
        

        //Game ends
        EndDrawing();
    }
    
}

Anyone please help me pleaseā€¦
I made a game that i can close it by pressing X key or ESCAPE key, until this tutorial everything was good, but when i debug it and run it, I cant move the circle to left and right by pressing ā€œAā€ or ā€œDā€, nothing happen by pressing these keys. please show me the way.

It my code:

#include "raylib.h"

int main()
{
    // Window layout
    int width{350};
    int hight{200};
    InitWindow(width, hight, "Halim Shams");

    SetTargetFPS(60);
    while (WindowShouldClose() == false)
    {
        // Circle demations
        int circle_x{175};
        int circle_y{100};

        BeginDrawing();
        ClearBackground(WHITE);
        DrawCircle(circle_x, circle_y, 25, BLUE);

        // Game starts

        if (IsKeyDown(KEY_A))
        {
            circle_x = circle_x - 10;
        }

        if (IsKeyDown(KEY_D))
        {
            circle_x = circle_x + 10;
        }
        
        

        //Game ends
        EndDrawing();
    }
    
}

Hi there!

The key to the problem is with these two lines

int circle_x{175};
int circle_y{100};

With every iteration of the loop (aka ā€˜tickā€™) you are re-initializing your circle_x and circle_y variables to the same value, essentially erasing anything you do with your A and D inputs.

These variables need to be declared and initialized outside of the while loop.

Privacy & Terms