C++ libraries...!

Hi all, I am just starting out using external libraries. I can compile and link the below code, for example. And I can produce and use library no problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// mylib.h
#ifndef MYLIB_H
#define MYLIB_H
double calcSqrt(double);
#endif

// calc.cpp
#include <math.h>
#include "mylib.h"
double calcSqrt(double d)
{
        return sqrt(d);
}

// main.cpp
#include <stdio.h>
#include "mylib.h"
int main()
{
        double d = 100;
        printf("sqrt(%3.0f)=%2.0f\n",d,calcSqrt(d));
        return 0;
}


The only problem now is the next library I want to use (Aquila) has no equivalent of calc.cpp. ie I need to use the 1x master header file = master.h and my own main.cpp to build this library. I wonder does anyone have any tips? I tried to compile master.h to produce master.cpp but this cannot be right.

Sorry if this not well explained. Thank you
From the library's download page:

Aquila relies on CMake as a build tool, so make sure you have it installed (version 2.8 or later).


Do you have CMake installed? If so there are some instructions for the build.
Thank you, will try this!
Solved, thank you
Boost is another library you might want to look at, easier to install and deal with IMO.

Actually a lot of combined libraries. And a lot of newer C++ features started out being in Boost before being adopted into the C++ standard.

https://www.boost.org/

Boost builds itself, the instructions are easy to follow. Plus there are a lot of header-only Boost features.

If you want to get used to dealing with 3rd party libraries Boost is one of the best.
Thank you! I was unaware of this huge library!

I am using mingw, so install is a bit more complex, but I found a github method which works
(search zrsmithson if anyone else needs it)

So now I have (many) header files:

C:\boost\include\boost-1_62\boost

And (many) Lib files:

C:\boost\lib

And an example.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//#include <boost/regex.hpp>  //via boost example
#include "C:\boost\include\boost-1_62\boost\regex.hpp"    //my machine
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}


So a standard C++ library format I guess.

Problem is I can't get this to compile! I know I need to supply the header files to compiler and the lib files to the linker, but am very confused as to how. I have this problem with other libraries too so it is my lack of knowledge...

eg I think it is something like this...

First of all convert example.cpp to object:
g++ -c example.cpp -o example.o


//other stuff here, not sure


Now link it:

g++ example.o -I. -L. -l -o example.exe

I am just not sure what flags to use. eg

-LC:\boost\lib

-IC:\boost\include\boost-1_62\boost

But I am not sure and pretty confused, especially coming from Python!

Any tips gratefully received

So now I have (many) header files:

C:\boost\include\boost-1_62\boost


The latest version of boost is 1_77 :
https://www.boost.org/

I am guessing you literally followed the instructions on the page you found.

C++ already has regex, no need to use the boost one.

When compiling make sure to turn warnings on: they are your friend. It's worth reading the manual sometime there are other options not covered by -Wall -Wextra . And specify the standard.

g++ -std=c++20 -Wall -Wextra

as a minimum. The standard should ideally be the latest that your version of compiler will cope with. One can find out the version of g++ with :

g++ -v
Last edited on
C++ already has regex, no need to use the boost one.

According to the boost reference ans SO c++11 regex doesn't support lookbehinds so there is still a need for boost::regex.
https://theboostcpplibraries.com/boost.regex => see comment at bottom
https://stackoverflow.com/questions/14538687/using-regex-lookbehinds-in-c11
Topic archived. No new replies allowed.