How to use spdlog - c++

I am new to c++, please don't grill too much. I am trying to use spdlog in C++ program and using CLion from Jetbrains.
Here is my screenshot but don't know what I am doing wrong. Please advise what I am doing wrong .
This is my main.cpp
#include <iostream>
#include "include/spdlog/spdlog.h"
int main() {
std::cout << "Hello, World!" << std::endl;
spdlog::info("hello world");
return 0;
}
This my CMakeList.txt
cmake_minimum_required(VERSION 3.17) project(Lesson01)
set(CMAKE_CXX_STANDARD 14)
add_executable(Lesson01 main.cpp) include_directories(spdlog)
Thanks

In the prototype you have created you have mentioned twice the keyword include
First after the # and a second one in between the quotation mark.
I have been looking in internet and basic writting of spdlog prototype is just. Here the link: https://github.com/gabime/spdlog
#include "spdlog/spdlog.h"
Test is without the second include in your code.
All the best
Mathieu

In your code you have #include "include/spdlog/spdlog.h", and going by the screenshot you linked, this include is found. However it is then complaining that spdlog.h tries to #include <spdlog/common.h>, and this file is not found.
Together this sounds like you are not setting the right include directory for the library, and just getting the first file right by over-specifying the path.
I would try to change include_directories(spdlog) to include_directories(include) (or possibly include_directories(include/spdlog), not entirely sure which folder is the base one with the library in). If you want, then you can also add all of those; I don't think anything will break from adding too many here, but it may impact compile speed so try to only keep the correct one.
Also, after making this change you may need to change your original include from #include "include/spdlog/spdlog.h" to #include "spdlog/spdlog.h".

Related

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.

CLion doesn't resolve headers from external library

Some time ago I started a big header library in C++1x using XCode. The current layout of the library is () something like (partial output from ls -R sponf)
sponf/sponf:
ancestors sponf.h sponf_utilities.h
categories sponf_children.h utilities
children sponf_macros.h
sponf/sponf/ancestors:
function.h meter.h set.h simulation.h
sponf/sponf/categories:
free_space.h prng.h random_distribution.h series.h
sponf/sponf/children:
distributions histogram.h random simulations
meters numeric series spaces
sponf/sponf/children/distributions:
arcsine_der.h exponential.h
box_muller.h uniform.h
sponf/sponf/children/meters:
accumulator.h timer.h
#... other subdirs of 'children' ...
sponf/sponf/utilities:
common_math.h limits.h string_const.h
#... other directories ...
I wanted to port this project to CLion, which seems a really good IDE (based on the similar AndroidStudio IDE) but I'm getting some troubles.
Small test program
I tried this small program as a test:
#include <iostream>
#include <sponf/sponf.h>
using namespace std;
int main() {
using space = sponf::spaces::euclidean_free_space<double, 3>;
sponf::simulations::random_walk<space> rw;
rw.step(1);
std::cout << rw.position.value << std::endl;
return 0;
}
The program compiles and runs fine. However, CLion does not recognize the spaces namespace (declared in one of the children files), nor the simulations namespace; they are both marked red and I cannot inspect their content, nor navigate to their definitions by ⌘-clicking, etc. etc...
Relevant parts of the library
Looking in "sponf.h" we find
#ifndef sponf_h
#define sponf_h
/* The classes below are exported */
#pragma GCC visibility push(default)
// include some of the standard library files
// ...
#include <Eigen/Eigen>
#include "sponf_macros.h"
#include "sponf_utilities.h"
#include "sponf_children.h"
#pragma GCC visibility pop
#endif
while in "sponf_children.h" (which is located at the top level, next to "sponf.h") we find
#ifndef sponf_locp_sponf_children_h
#define sponf_locp_sponf_children_h
namespace sponf {
// include some of the children
// ...
#include "children/spaces/euclidean_free_space.h"
#include "children/simulations/random_walk.h"
// include remaining children
// ...
}
#endif
Each "child" header will then include its corresponding "ancestor" or "category" header (which defines the superclass of the "child" itself).
The reaction of CLion
Despite the autocompletition prediction, which easily finds all the subdirectories and the headers, all the include directives in this last file get marked red and ⌘-clicking on any of them leads to a popup message
Cannot find declaration to go to
while the right ribbon of the editor signal many errors like
',' or ) expected
) expected
Declarator expected
Expecting type
Missing ;
Unexpected symbol
which are not the same for each include statement (each generates from 2 to all of these errors).
On the other hand, CLion is perfectly able to find all Eigen headers, which have pretty much the same structure!
I have put both libs in /opt/local/include and changed CMakeLists.txt accordingly
cmake_minimum_required(VERSION 2.8.4)
project(sponf)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(/opt/local/include/sponf /opt/local/include/eigen3)
set(SOURCE_FILES main.cpp)
add_executable(sponf ${SOURCE_FILES})
Why can't CLion properly parse the project structure? XCode, after having included /opt/local/include/sponf and /opt/local/include/eigen3 in the HEADER_SEARCH_PATHS env. variable of the project, is able to find any header while compiling the same exact program.
Is there anything else I need to know? Am I doing it wrong or is it that CLion isn't that mature yet and this is just a sorry bug? This is my first approach to the CLion and the CMake toolchain, so any kind of information about it will be greatly appreciated!
Sorry for the very long question, I didn't manage to shrink it further... Thanks in advance guys, see you soon!
Here what I did in windows using cigwin64. I wanted to use Eigen library include in my project.
Eigen library is places in /usr/include/eigen then edited CMakeLists.txt and add
include_directories("/usr/include/eigen")
into it. Now CLion can find all source files in eigen lib. May be this what you wanted too.
Downgrade to Clion 2016.1.4 fixes the problem

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

How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible.
I'm trying to do pattern matching in C++.
I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE).
I've also downloaded a pcrecpp.h header file and have it in the same directory I'm writing the following code (not writing actually. I'm coping example code from a PDF tutorial named PCRE- Perl Compatible Regular Express).
But I can't get it to work. The code is as follows:
#include <iostream>
#include <string>
#include <pcrecpp.h>
using namespace std;
int main()
{
int i;
string s;
pcrecpp::RE re("(\\w+):(\\d+)");
if (re.error().length() > 0) {
cout << "PCRE compilation failed with error: " << re.error() << "\n";
}
if (re.PartialMatch("root:1234", &s, &i))
cout << s << " : " << i << "\n";
}
When I compile the code, Dev-C++ gives me a lot of errors including: "`pcrecpp' has not been declared" and "RE" undeclared.
How should I deal with the downloaded files and fix my problem? Or is there something obvious that I'm missing?
If you specify the file for #include with angle brackets (<>), then the compiler will only look for that header in the locations for external libraries, in so far as the compiler is aware of them.
If you instead use quotation marks (""), then the compiler will also look in the locations for the current project, which typically includes the current directory.
The quick fix for your current problem is to use
#include "pcrecpp.h"
The alternative is to tell the compiler where it can find the headers of the PCRE library.
You will have to tell the compiler where it can find the headers of the PCRE library.
How to do this differs from build system to build system, but if you are using an IDE, then there should be an option somewhere to specify the 'Include directories'. This is where you add the directory of the PCRE headers (with full path).
As a side-note: When the compiler gives you a large number of errors and warnings, always start with fixing the first one. I would guess that in this case it was something like "unable to find header: pcrecpp.h".
It is often the case that, if the compiler tries to continue after encountering a problem, more problems are found that are follow-on problems of the first one. When the first problem is fixed, these also magically disappear.
g++ -lpcrecpp ......
you need to add '-lpcrecpp' to g++ command
cout << “PCRE compilation failed with error: “ << re.error() << “\n”;
I just copied your code and tried to compile it. I got the same error as you reported.
The problem is that string you put to cout is not properly started/ended. You should use real " instead of marks which looks like double quotes (") but it is not. If you fix it, your code should compile w/o any error.
You have included
#include <pcrecpp.h>
1st point to check But is file in the inlcude path of your code. Did you download the installable ? Check where it has been installed on your machine.
2nd point is to check do you have the library paths defined, so that they can be resolved during compiling and linking.