"error: name must be a namespace name" when using 'clibgen.g​enerateLib​raryDefini​tion' to generate Matlab interface to a C++ library

9 views (last 30 days)
I'm trying to to use Matlab to call functions from a C++ library. The library contains functions to control a scanning mechanical motor.
However, Matlab reminds the error "name must be a namespace name" when using the function 'clibgen.generateLibraryDefinition' , which refers to the namespace part of the head file
"using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::IO::Ports;"
I guess there is a system file named 'system.h' or so that couldn't not be identified by Matlab. I have no idea how to solve this.
Here are the codes I used in matlab.
%%%
libname="haha";
a1="C:\Users\Administrator\Documents\User folder 2021\Lei\Projects\MotorControl_P_04-10-22\Motor\Test2\CppSKSample\SKSampleClass.h";
b1='C:\Users\Administrator\Documents\User folder 2021\Lei\Projects\MotorControl_P_04-10-22\Motor\Test2\CppSKSample\Debug\CppSKSample.lib';
c1='C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\x86_64-w64-mingw32\include\windows.h';
clibgen.generateLibraryDefinition([a1],"Libraries",b1,"PackageName",libname)
Here is part of the head file.
%%%
#pragma once
#include <string>
#include <vector>
#include <cmath>
#include <stdexcept>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::IO::Ports;
public ref class SKSampleClass
{
public:
enum class Controller_Type
{
SHOT_302GS,
SHOT_304GS,
GIP_101B,
GSC01
};
Here are the error message from matlab.
Errors parsing interface generation files.
C:\Users\Administrator\Documents\User folder 2021\Lei\Projects\MotorControl_P_04-10-22\Motor\Test2\CppSKSample\SKSampleClass.h(18):
error: name must be a namespace name
C:\Users\Administrator\Documents\User folder 2021\Lei\Projects\MotorControl_P_04-10-22\Motor\Test2\CppSKSample\SKSampleClass.h(19):
error: name must be a namespace name
C:\Users\Administrator\Documents\User folder 2021\Lei\Projects\MotorControl_P_04-10-22\Motor\Test2\CppSKSample\SKSampleClass.h(19):
error: expected a ";"

Answers (1)

Maneet Kaur Bagga
Maneet Kaur Bagga on 17 Nov 2023
Hi Lei,
As per my understanding, the error message suggests that the error is due to the namespaces in the C++ header file when using "clibgen.generateLibraryDefinition".
To resolve the error, the header file can be modified by removing the "using" statements and fully qualify the namespaces by referencing them explicitly. Please refer to the modified code below:
#pragma once
#include <string>
#include <vector>
#include <cmath>
#include <stdexcept>
public ref class SKSampleClass
{
public:
enum class Controller_Type
{
SHOT_302GS,
SHOT_304GS,
GIP_101B,
GSC01
};
};
Please refer to the clibgen MATLAB documentation for more information.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!