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

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?

Related

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.

Using "string" header file gives error in c++ on Windows 10

So I recently upgraded Windows 8.1 to Windows 10. I didn't faced this type of error before but when I program in c++ and include "string" header file then it pops out an error when executing though compiling does not give any error. I tried using "cstring" and update Microsoft Visual C++ Redistributable but no change. If I don't use "string" header file then it will not produce any error.
Here is test.cpp
#include <iostream>
#include <string>
using namespace std;
int main(){
string text = "Hello World!";
cout << text;
return 0;
}
On compiling it does not give error
g++ test.cpp -o test.exe
But when I tried to execute via console it doesn't print anything.
And when I tried to execute by double clicking "test.exe" then it showed me this
Finally I found what causes this error.
There was a faulty app which is set in my system path variable containing a faulty libstdc++-6.dll which causes this error. I remove the path from th system path variable and now everything working fine.

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.

Error C1083: Cannot open source file: "stdafx.cpp': No such file or directory

I have the above error and have followed all solutions but does not fix.
This is my stdafx.h file. I have it in the target directory, I can see the file and the full path goes to there. In addition, I have created a win32 project. Why is my code not working?
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
ACTUAL CODE:
// ConsoleApplication1.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
int main() { std::cout << "Hello world!" << std::endl; return 0; }
For the record, I had this problem when cloned a repo using git from the Ubuntu shell in Windows 10.
After I cloned the repo to a different location usin git from a Windows shell, the problem went away and the solution builds fine.
The issue was that the build scripts were looking for 'stdafx.cpp' in a folder with uppercase name (C:\GIT\LIBRARYX\SRC\) and this path did not work if the solution was cloned using git from Ubuntu bash.

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.