How to add muparser in the project with nix

i have to calculate the underlying area of a function, to get the function from the keyboard i want to use the library muparser but i get this error:

clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:10: main] Error 1

the code main.cc is:

#include <iostream>
#include <muParser.h>
#include <cmath>

using namespace std;

double funzione(double x, mu::Parser& parser) {
    parser.DefineVar("x", &x);
    return parser.Eval();
}

double calcolaAreaTrapezi(double a, double b, int n, mu::Parser& parser) {
  
     double h = (b - a) / n;
    double result = 0.5 * (funzione(a, parser) + funzione(b, parser));

    for (int i = 1; i < n; ++i) {
        double x = a + i * h;
        result += funzione(x, parser);
    }

    return result * h;
}

int main() {
    mu::Parser parser;

    // Input della funzione dall'utente
    string espressione;
    cout << "Inserisci la funzione in termini di x: ";
    getline(cin, espressione); //getline input da tastiera di str con " "

    parser.SetExpr(espressione); //input con parser

    // Input dell'intervallo di integrazione [a, b] e del numero di trapezi n
    double a, b;
    int n;

    cout << "Inserisci l'estremo inferiore a: ";
    cin >> a;

    cout << "Inserisci l'estremo superiore b: ";
    cin >> b;

    cout << "Inserisci il numero di trapezi n: ";
    cin >> n;

    // Calcolo e stampa dell'area
    double area = calcolaAreaTrapezi(a, b, n, parser);

    cout << "L'area sottostante alla curva è: " << area << endl;

    return 0;
}

replit.nix:

{ pkgs }: {
	deps = [
		pkgs.clang_12
		pkgs.ccls
		pkgs.gdb
		pkgs.gnumake
    pkgs.muparser
	];

  buildInputs = [ pkgs.muparser ];

  CPPFLAGS = "-I${pkgs.muparser}/include";
  LDSFLAG = "-L${pkgs.muparser}/lib";
  
}

can anyone help me? thx

Try edit your replit.nix file (inside the pkgs) to look like this:

{ pkgs }: {
	deps = [
		pkgs.clang_12
		pkgs.ccls
		pkgs.gdb
		pkgs.gnumake
    pkgs.gcc
    pkgs.muparser
	];
}

And run the project again

1 Like

[quote=“xegi, post:3, topic:97278, full:true”]
Sorry for the delay in my reply…
However even using the GCC package it keeps giving the same error, it seems that it can’t access the Muparser library commands. Although it doesn’t give loading errors the nix… doesn’t recognize muparser commands (i think)

Can you share the link to your repl?

I’ve put you in the project and you can edit and test it if you want

Done. You can test it now.

1 Like

@xegi did it work? If it did, mark this as solution. To clarify what I did, you had an typo in your makefile, you wrote LDSFLAG when the correct one is LDFLAGS (the typo was inside your replit.nix file too).

Another thing is that you need to tell the linker to actually use the muParser library, so I included the -lmuparser to your linker flags.