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

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.

Related

[C++ Library]: "file or folder does not exist" when compiling

I want to develop a small application which uses some libraries. So I downloaded them and placed the include files in a folder called include.
For my application, I used cpprestsdk, but my question shouldn't be limited only to this library.
This is a rough example of my folder Structure:
myproject
include
cpprest
...
pplx
...
test.cpp
And this is my Code:
#include <iostream>
#include "include/cpprest/http_client.h"
#include "include/cpprest/filestream.h"
#include "include/cpprest/json.h"
int main() {
// code
std::cout << "Hello World!";
return 0;
}
which results in followin error code when compiling (g++ test.cpp -o test) with g++ or gcc (on Ubuntu):
<error needed>
What have I done wrong? when I inspect the file mentioned in the error message, then I notice, that all includes in the library are like so #include "cpprest/asyncrt_utils.h". As you can see, it refers to the file as it were in a subfolder called cpprest, which it is not. It is located with the other file in the same folder. I guess that results in my problem. My question now is: how do I fixe this issue?

"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.

how to compile box2d helloworld correctly

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

Include paths not found while compiling with g++ on MacOS

I'm trying to compile the simplest program on MacOS 10.6 like:
$ g++ -o hello hello.cpp
the following source:
#include <iostream>
int main (int argc, char * const argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
I'm getting the error:
hello.cpp:1:20: error: iostream: No such file or directory
hello.cpp: In function ‘int main(int, char* const*)’:
hello.cpp:4: error: ‘cout’ is not a member of ‘std’
So obviously I have to add the include path somewhere. My question is where can I find the include directories and how can add them globally (I don't want to provide the include path whenever I want to compile).
I just installed the XCode 3.1.4 and managed to compile it via Xcode, but not via command line. I found some header files in this directory:
/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers
and tried to add it to the HEADER_SEARCH_PATHS after reading this question, but no luck.
I'm developing on Linux and everything is working fine there, but I want to continue doing that on MacOS. Any help?
On my Mac, that include file is in /usr/include/c++/4.0.0/iostream . Are you sure
you have all the command-line development tools installed? They might not be by default;
I'm pretty sure I had to install it manually when I first set up my Mac. There should be a "developer tools" package somewhere on your OS X installation media.
Or, if you want to make sure you're getting the latest version, you can download it from:
http://developer.apple.com/technology/xcode.html
$ g++ -o example.bin example.cpp //to compile
$ ./example.bin //to run
It's code:
#include <iostream>
using namespace std;
int main () {
cout << "Hello, World!\n";
return 0;
}