Compile C++ with static lib - c++

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.

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?

C++ - no rule to create target SaltPepper.o

I know there are several questions like this, no answer I have found so far seems to solve my problem.
I am using eclipse.
I coded something in the main function, and it worked fine.
I then an external function with the code in the main and now I get funny mistakes.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <random>
#include <ctime>
#include <cstdlib>
using namespace std;
using namespace cv;
void createSaltandPepper();
int main(int argc, char** argv) {
createSaltandPepper();
return 0;
}
No, as I am trying not to give to extensive an example again, here is the outside of the function:
void createSaltandPepper() {
//mycode
}
At least it does not seem to be a spelling mistake.
However, the error is:
make all
make: *** No rule to make target 'SaltPepper.o', needed by 'Display'.
I do wonder why it is trying to make a tagert called SaltPepper.o if my function is called createSaltandPepper.
Can somebody help me?
Edit: I did not conciously create a makefile, as I said I am working with eclipse.
And again, the code works fine as long as it is inside the main function instead of inside createSaltandPepper().
My file is called DisplayImage.cpp, the code above is in this file, including the function createSaltandPepper();
The only two function in this file are the main function and createSaltandPepper();
There are no other source files in the project.
I am sorry if I come across as rather stupid: I am a Java programmer and have not a clue about makefiles and such.
See this screenshot:
I now created a new project and added my old file into it, this time calling it Display.cpp
"build all" resulted in an error in subdir.mk which reads:
subdir.mk:18: recipe for target 'Display.o' failed
make: *** [Display.o] Error 1
The complete Error message for the build is as follows:
make all
Building file: ../Display.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0-std=c++11 -MMD -MP -MF"Display.d" -MT"Display.d" -o"Display.o" "../Display.cpp"
subdir.mk:18: recipe for target 'Display.o' failed
g++: error: argument to ‘-fmessage-length=’ should be a non-negative integer
make: *** [Display.o] Error 1
Update
I switched to Netbeans.
It now works.
First, this error has nothing to do with compilers, this error message is produced by the build system (make in your case). Basically, eclipse seems to call make, which figures out what are dependencies between files and which of them need recompiling and in turn calls the compiler to compile them. Every C++ source file is then compiled to an object file .o (typically with the same name: SaltPepper.cpp -> SaltPepper.o). Then all the object files are linked together with the libraries to form the final executable.
This particular error message tells you that your executable is specified to depend on the object file SaltPepper.o, but the build system does not know how to produce it. Most likely there is no corresponding SaltPepper.cpp file. So check if such file exists. If not, check the settings of your project (or the Makefile if you created it manually) and see if all the cpp filenames are specified correctly. You should add all the cpp files with your code to the project, and remove all the extraneous cpp files.
Update: Note that build system operates on the file level. It does not know anything about functions etc., only source files. Maybe this is the source of the confusion.

How do I include Boost.Interprocess and Boost.DateTime properly?

This is a really basic question because I am a C++ newbie. I want to use the Boost.Interprocess library, but am having trouble building it. I'm trying to follow these instructions, but it's not working for me. Here is what I have:
#define BOOST_DATE_TIME_NO_LIB
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
using namespace std;
int main() {
cout << "Hello, beautiful world!\n";
}
But I get this error:
boost_1_55_0\boost\date_time\gregorian_calendar.hpp(63) : fatal error C1083: Cannot open include file: 'boost/date_time/gregorian_calendar.ipp': No such file or directory
I know Boost is able to load properly, because I can get an example that uses #include <boost/lambda/lambda.hpp> to work just fine. It's just when I try to include the Boost.Interprocess library that I am having trouble. The cause is clearly because it's having trouble including the Boost.DateTime library properly, but according to the documentation (linked above) I should be able to get by without separately compiling Boost.DateTime if I define BOOST_DATE_TIME_NO_LIB, right?
What am I missing here?
You need to add it to the preprocessor
In VS go to - Project >> properties >> C/C++ >> Preprocessor in the 'Preprocessor Definitions' paste BOOST_DATE_TIME_NO_LIB.
You can download boost libraries here: https://www.boost.org/users/download/
After that, you can include them in your projects. Also, you can check this video on how to add boost libraries in eclipse IDE on Ubuntu: https://www.youtube.com/watch?v=gN8zrnWxFeI

undefined reference to function code blocks

main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help

compiling a C++ class in Xcode: error during compilation: stl vector

I have a C++ class that compiles fine on linux with gcc and on widows in visual studio.
boid.h:
#ifndef BOID_CLASS_HEADER_DEFINES_H
#define BOID_CLASS_HEADER_DEFINES_H
#include "defines.h"
class Boid {
public:
// Initialize the boid with random position, heading direction and color
Boid(float SceneRadius,float NormalVel);
.....
protected:
...
};
#endif
and in boid.cpp:
#include "Boid.h"
// Initialize the boid with random position, heading direction and color
Boid::Boid(float SceneRadius,float NormalVel)
{
....
}
However, I get the following error when I compile this code in Xcode:
Compiling Boid.h: "error: vector: No such file or directory"
Any ideas? I thought you could take C/C++ code and compile it in Xcode without issues?
Thanks
EDIT: Added defines.h (also added #endif to sample, but that was in the original code)
EDIT 2: I am getting a different error after a commenting out a couple of includes there were empty: the vector error above.
#ifndef BOID_NAV_DEFINES_H
#define BOID_NAV_DEFINES_H
#include <stdlib.h>
#include <vector>
#include "Vector3d.h"
#include "Point3d.h"
#include "win_layer.h"
#endif
Are you including the C++ header in a .m file?
.m files are treated as .c files with Objective-C extensions.
.mm files are treated as .cpp files with Objective-C extensions, then it's called Objective-C++
Just rename any .m file to .mm, right-click or ctrl-click and select rename on the file in Xcode.
Without changing any .m to .mm or anything like that, if you click your project, click tagets->build settings
go all the way down to "LLVM GCC 4.2 - Languages" (new xcode says "Apple LLVM compiler 4.2") you will see Compile Sources As change that value to Objective-C++;
I hope this will help.
After updating xCode to version 10, I have had issues including < map > and < vector > libraries. Found an easy solution by changing the C++ library type in the project's build settings (target's Build Settings):
C++ Standard Library: libc++ (LLVM C++ standard library with C++ 11 support)
Compiled without any problem.
Make sure you're compiling it as C++. Right click the file in XCode and select Get Info and make sure that File Type is set to sourcecode.cpp.cpp for the implementation files.
Assuming you're talking about the OS X XCode, that uses gcc to do the actual compiling. So there should be no difference between that and Linux, other than maybe different versions of gcc.
First thing that jumps out at me here is that you've typed "boid.h" as the name of the file, but you're including "Boid.h". Assuming that's not a typo, I would expect that to cause trouble on both Linux and OS X....
Edited to answer the new question: Hmmm... vector is definitely part of Xcode: /Developer/SDKs/MacOSX10.5.sdk/usr/include/c++/4.0.0/vector on my machine.
Further thought: If you port the source and makefiles from the Linux build over to the Mac, you can probably just compile it from the command line exactly like you do on Linux....
Definitely, something in defines.h is affecting the class definition.
This issue had two errors:
one of my includes had a typo which caused a compile error
the vector not found error was fixed by the .m files to .mm
Not sure if you forgot to paste, but you have an unterminated #ifndef
What's inside defines.h ?
Edit: You seem to have found the solution. One more remark:
#include <stdlib.h>
For C++, please:
#include <cstdlib>
:D