0

I'm compiling some C++ with emscripten and I'm having a problem to use some features from gsl such as "Expects". The emcc can't find "gsl/gsl". How can I compile it with gsl?

Here is the header file:

#ifndef ROBOT_CPP_PWMTEST_H
#define ROBOT_CPP_PWMTEST_H

#include <iostream>
#include <gsl/gsl>

class PwmTest {

public:
    int duty_cycle_pwm_a;
    int duty_cycle_pwm_b;
    explicit PwmTest();
    ~PwmTest();
    int setPwmA(int duty_cycle);
    int getPwmA();
};


#endif //ROBOT_CPP_PWMTEST_H

Here is the source file:

#include "PwmTest.h"

PwmTest::PwmTest()
{
    duty_cycle_pwm_a = 0;
    duty_cycle_pwm_b = 0;
    std::cout << "PWM instance Created" << std::endl;
}

PwmTest::~PwmTest()
{
    std::cout << "PWM instance Destroyed" << std::endl;
}


int PwmTest::setPwmA(int duty_cycle)
{
    Expects(-1 < duty_cycle && duty_cycle <= 100);
    duty_cycle_pwm_a = duty_cycle;
    std::cout << "PWM A is set to " << duty_cycle_pwm_a << std::endl;
    return 0;
}

int PwmTest::getPwmA()
{
    std::cout << "PWM A value is: " << duty_cycle_pwm_a << std::endl;
    return duty_cycle_pwm_a;
}


#ifdef __EMSCRIPTEN__

#include <emscripten.h>
#include <emscripten/bind.h>

EMSCRIPTEN_BINDINGS(pwm_class_emscripten) {
    emscripten::class_<PwmTest>("PwmTest")
    .constructor<>()
    .function("setPwmA", &PwmTest::setPwmA)
    .function("getPwmA", &PwmTest::getPwmA)
    .property("duty_cycle_pwm_a", &PwmTest::duty_cycle_pwm_a)
    ;
}
#endif

It works if I take the gsl off of it.

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • `gsl/gsl` doesn't look like a filename for a header. – Yksisarvinen Jul 05 '22 at 22:15
  • 1
    https://github.com/Microsoft/GSL#quick-start https://github.com/Microsoft/GSL#using-the-libraries may provide some insight. – Ed Dore Jul 05 '22 at 22:59
  • @Yksisarvinen That part seems correct. This is the C++ code guidelines support library implementation. I think they intentional chose the standard library style for headers (without file endings). – user17732522 Jul 06 '22 at 00:11
  • the gsl/gsl works fine, and I use the Fetchcontent for the CMakeLists file, as you have suggested on the link, that works good as well. – Thiago Souto Jul 06 '22 at 01:27
  • But when I'm compiling with `emcc PwmTest.cpp -o test.html -std=c++17 --bind ` it gives the error ```./PwmTest.h:9:10: fatal error: 'gsl/gsl' file not found #include ^~~~~~~~~ ``` – Thiago Souto Jul 06 '22 at 01:28
  • I tried to include -I and the path, but I have some spaces on the folder. How do you include spaces on the path? – Thiago Souto Jul 06 '22 at 01:29
  • or how to make it with relative paths – Thiago Souto Jul 06 '22 at 01:29
  • 1
    @ThiagoSouto You enclose the whole path with `"` or put a backslash before the space. – user17732522 Jul 06 '22 at 02:17

0 Answers0