how to compile box2d helloworld correctly - c++

I'm come back to c++ (I haven't got using it since some years) to learn myself box2d API.
I precise that I'm on Linux system (Ubuntu) and I have installed box2d(2.3.0) from source following this guide.
So, I try to compile the box2d included helloworld code but I got an error on instance of b2world.
undefined reference to b2world
There is the complete file.
I have try with a very more basic test but the result is the same :
#include <iostream>
#include <Box2D/Box2D.h>
using namespace std;
int main()
{
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
cout << "Hello world!" << endl;
return 0;
}
I have no error including box2d.h and no error using b2Vec2, so I think box2d is installed correctly on my computer.
And I have verified that b2World is specified in box2d.h (it's the original one).
I have try to compile using g++ command and with the IDE Qt Creator but no one succeed.
Any help or suggestion is highly appreciated.

The problem was I haven't got specified the path to box2D library.
So, I succeed to compile and execute my little hello world program following this steps :
1 ) Copy and paste the library into my project folder (I put it into a lib directory).
2 ) add path of the lib directory to $LD_LIBRARY_PATH :
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
3 ) compiling with g++ and specify to use the box2D library :
g++ main.cpp -o test -l Box2D

Related

Content of directory messes with how the code runs even though there's no reference of it

So I spent 2 hours trying to narrow down the cause of my code not working and I think it might just be something weird. Here's the exact example code I have and I can't minimize it further (yes, bar does literally nothing) :
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
void bar()
{
// do stuff...
}
int main()
{
std::cout << "please run" << std::endl;
std::thread t(bar);
t.join();
std::cout << "completed.\n";
return 0;
}
Here's how I build it :
g++ -std=c++0x -o test test.cpp -pthread
When all of this is done in a blank directory, it works perfectly, but if I put test.cpp in my project directory, compile it here and run it, it crashes before even entering main. Here's the crash report but I translated it from French so it's not the exact text:
"The entry point of the procedure std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) cannot be found in the library of dynamic links [project directory]\test.exe"
Note that this message did not appear in the console command but in a separate window pop-up from where I can't copy-paste.
Here's what's the project directory is (not quite exactly because script files were updated but it's the same structure and libraries) : https://github.com/Leroymilo/RTX_Cpp
It has SFML and jsonCpp in it if it changes anything.
Also I am on Windows10 and I'm using VScode if it makes any difference.
I did what's advised in this post : Segmentation Fault while creating thread using #include<thread> but it does not change the result.
The issue was the presence of a file called "libstdc++-6.dll" in my project directory. I have no memories of why it's here because I copied all libraries from another project but if anyone does the same mistake, here's your solution.
Edit : I found out about why I had this in my files : it's because my build wasn't static and launching the built executable on another computer showed a pop-up asking for these. I fixed my static build issue with the answers provided in this post : Issues with libgcc_s_dw2-1.dll and libstdc++-6.dll on build .

Add SFML (third party library) to C++ project on Xcode

I'm learning how to build a simple UI in C++ on my Mac (OS 11.6) using Xcode.
As first step I'm compiling the "Hello world" program, my problem is that the build on Xcode fails but write my own command from terminal, instead, works.
This is the program, I'm using SFML :
#include <iostream>
#include "SFML/Graphics.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I have no error here but when launching Run from Xcode this is the output, in Graphics.hpp file :
#include <SFML/Window.hpp>. //'SFML/Window.hpp' file not found
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/CircleShape.hpp
//other header files
This is how the project is structured ("TestGui" is the project name) :
-TestGui.xcodeproj
-TestGui(folder)
--SFML(directory with all headers file available
-- main.cpp
SFML source code here
So I tried to compile it with my own hands from terminal with :
g++ main.cpp -I ./SFML -o main
and
clang++ main.cpp -I ./SFML -o main
In both cases it compiled, also run worked.
Since the error is linked to a file not found I tried to tell it where libraries are located, so in Xcode from Product->Scheme->Edit Scheme->Run->Arguments->Arguments passed on launch : added -I ./SFML. But the error is still alive.
Added SFML folder to targets from Xcode, didn't copy-pasted but maybe I did it wrong, this is my first time.
EDIT : SFML folder:
--SFML
--- many .hpp files
--- 5 folders (Audio, Graphic, Network, System and Window)
I tried to add also this argument : -L ./SFML but nothing.

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

"undefined reference to `WinMain#16'" Error in gcc editor

I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.