Build/compile errors because of compilerOptions in legacy_code - matlab

I want to create some trivial S-function with legacy_code for my MexTest.c:
legacy_code('compile', specs, compilerOptions)
There are 2 macros (TEST_DEF1 and TEST_DEF2), which I want to put into compilerOptions. The 2 macros are used in the function TestFnc().
But somehow, the TestFnc() does not see the 2 macros defined in compilerOptions before...
MexTest.c:
#include "MexTest.h"
void TestFnc(double *p_u, double *p_y)
{
*p_y = *p_u + 1;
#ifndef TEST_DEF1
#error "undefined: TEST_DEF1"
#endif
#ifndef TEST_DEF2
#error "undefined: TEST_DEF2"
#endif
}
MexTest.h:
#ifndef MEXTEST_H_
#define MEXTEST_H_
void TestFnc(double *p_u, double *p_y);
#endif
legacy_code tool
specs = legacy_code('initialize');
specs.HeaderFiles = {'MexTest.h'};
specs.SourceFiles = {'MexTest.c'};
specs.SFunctionName = 'Test_SFunc_gen';
specs.OutputFcnSpec = 'void TestFnc(double u1[1], double y1[1])';
%% Compile C-MEX-s-function
legacy_code('sfcn_cmex_generate', specs);
legacy_code('generate_for_sim',specs);
legacy_code('compile', specs, {'-DTEST_DEF1', '-DTEST_DEF2'});
Mex Build Errors:
### Start Compiling Test_SFunc_gen
mex('-IC:\Users\xxxx\Documents\', '-c', '-outdir', 'C:\Users\xxxx\AppData\Local\Temp\tp05e00c12_2732_4dab_a0f8_f30bd05992f8', 'C:\Users\xxxx\Documents\MexTest.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
C:\Users\xxxx\Documents\MexTest.c: In function 'TestFnc':
C:\Users\xxxx\Documents\MexTest.c:8:10: error: #error "undefined: TEST_DEF1"
#error "undefined: TEST_DEF1"
C:\Users\xxxx\Documents\MexTest.c:12:10: error: #error "undefined: TEST_DEF2"
#error "undefined: TEST_DEF2"
How could I use the -D option correctly for my case?
(Matlab version 2021a)

Related

Including edk2-libc in efi shell application

How would one approach adding support for https://github.com/tianocore/edk2-libc, say I want to include stdio and use printf in my edk2 application? I followed StdLib/Readme.txt, and am able to successfully build examples in the AppPkg, however, when I try to add StdLib to my project I get errors like these:
LibString.lib(Searching.obj) : error LNK2005: strspn already defined in LibString.lib(Searching.obj)
LibCtype.lib(CClass.obj) : error LNK2005: isspace already defined in LibCtype.lib(CClass.obj)
(...)
LibC.lib(Main.obj) : error LNK2001: unresolved external symbol main
I do have the boilerplate (!include StdLib/StdLib.inc) added to my dsc file and in inf, I have StdLib.dec added to Packages and LibC and LibStdio added to LibraryClasses. I am using VS2017 toolchain for compilation and am using edk2-stable202108 release.
I was able to achieve this using below configuration for Hello Application of AppPkg.
Hello.inf
[Defines]
INF_VERSION = 0x00010006
BASE_NAME = Hello
FILE_GUID = a912f198-7f0e-4803-b908-b757b806ec83
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 0.1
ENTRY_POINT = ShellCEntryLib
#
# VALID_ARCHITECTURES = IA32 X64
#
[Sources]
Hello.c
[Packages]
MdePkg/MdePkg.dec
ShellPkg/ShellPkg.dec
StdLib/StdLib.dec
[LibraryClasses]
UefiLib
ShellCEntryLib
BaseLib
BaseMemoryLib
MemoryAllocationLib
LibStdLib
LibStdio
LibString
DevConsole
Hello.c
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <stdio.h>
int
main (
IN int Argc,
IN char **Argv
)
{
printf("Hello, world!\n");
return 0;
}
What I have understood is that LibC has ShellAppMain() defined in it which internally calls extern main(). So You need to provide definition of main() in your source just like I did in Hello.c

db2 external function ends with SQL0444N, Reason code 6, SQLSTATE 42724

I'll develop new (external) functions for DB2. My first test:
db2crypt.h
#ifndef DB2CRYPT_H
#define DB2CRYPT_H
#include <string.h>
#include <stdlib.h>
char *encryptAes(const char *source, const char *key);
#endif /* DB2CRYPT_H */
db2crypt.cpp
#include "db2crypt.h"
#include <string>
char *encryptAes(const char *source, const char *key) {
std::string test("abc");
return (char *)test.c_str();
}
is compiled without an error.
g++ -fPIC -c db2crypt.cpp -std=c++14
g++ -shared -o db2crypt db2crypt.o -L$DB2PATH -ldb2
I also copied the new file into $DB2PATH/function and made a softlink in $DB2PATH/function/unfenced.
Then I created the function with
create function aes(VARCHAR(4096), VARCHAR(4096))
SPECIFIC encryptAes
RETURNS VARCHAR(4069)
NOT FENCED
DETERMINISTIC
NO SQL
NO EXTERNAL ACTION
LANGUAGE C
RETURNS NULL ON NULL
INPUT PARAMETER STYLE SQL
EXTERNAL NAME "db2crypt!encryptAes"
which was also ok.
But when I do select db2inst1.aes('a', 'b') from SYSIBM.SYSDUMMY1
I get the error
SQL0444N Die Routine "DB2INST1.AES" (spezifischer Name "ENCRYPTAES") ist
durch Code in Bibliothek oder Pfad ".../sqllib/function/db2crypt", Funktion
"encryptAes" implementiert, auf die kein Zugriff möglich ist. Ursachencode:
"6". SQLSTATE=42724
(sorry, I don't know how to change the error output into english)
What I made wrong?
Ok, I got the answer.
Thank you at #mao, you did help me. But I also needed some other help. If someone searches for an answer:
First you have to compile with some important parameters:
g++ -m64 -fPIC -c <yourfile>.cpp -std=c++14 -I/opt/ibm/db2/V11.1/include/ -D_REENTRANT
g++ -m64 -shared -o <yourfile> <yourfile>.o -L$DB2PATH -ldb2 -Wl,-rpath,$DB2PATH/$LIB -lpthread
Second: The function declaration, you also have to add parameters for null values AND the return value can't be a function return, it has to be a parameter. Also you have to use the types which are defined in sqludf.h:
void SQL_API_FN encryptAes(SQLUDF_CHAR *source,
SQLUDF_CHAR *key,
SQLUDF_CHAR out[4096],
SQLUDF_SMALLINT *sourcenull,
SQLUDF_SMALLINT *keynull,
SQLUDF_SMALLINT *outnull,
SQLUDF_TRAIL_ARGS) {
...
}
Also, when you do C++ instead of C, you have to tell the script that it has to handle the function as C:
#ifdef __cplusplus
extern "C"
#endif
void SQL_API_FN ...

Swig -> Perl5 : Error compiling simplest SWIG-Module with VC15

I have a quite simple Swig Module for Demo purposes but can't get the wrapper/stub to get compiled.
Swigtest.h:
#pragma once
class Swigtest {
public:
Swigtest() {};
int func1(int param1);
};
Swigtest.cpp
#include "Swigtest.h"
int Swigtest::func1(int param1)
{
return param1 + 1;
}
Swigtest.i
%module Swigtest
%{
#include "Swigtest.h"
%}
%include "Swigtest.h"
The wrapper ist generated with swig.exe -c++ -perl5 Swigtest.i and then the compilation of the wrapper Swigtest_wrap.cxx fails with the following first error:
[path_to_perl_lib]\lib\core\win32.h(371): error C2061: syntax error: identifier 'STRLEN'
I'm using VisualStudio 2015 Toolset (acutally running on VS17) with Target Platform Version 8.1 and Swigwin 3.0.12.
Thank's for your help!

Load library error in Matlab

I am trying to use loadlibrary in MATLAB by importing the qliblibrary header file but when I use the load library command:
loadlibrary('qlib','qlib.h')
I get following error:
Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Program Files\MATLAB\R2017a\extern\examples\Legacy_code\qlib.h:168:7: error: #error
unsupported compiler/os combination
#error unsupported compiler/os combination
This is the qlib header file where I am getting the compiler error:
#pragma message ("used compiler: CVI LabWindows C")
#define QLIB_WIN_32
#define QAPICALLER _stdcall
#ifndef FAR
#define FAR
#endif
#ifndef WORD
#define WORD unsigned short
#endif
#pragma message ("destination OS: Windows 9x / NT / 2000 / XP")
#pragma message ("executable type: CVI")
#elif defined (__GNUC__) /* gcc (on Linux) */
#define FAR
#define far
#ifdef __unix__
#define QLIB_LINUX
#define QAPICALLER
#else
#error unsupported compiler/os combination
#endif
#else
#error unsupported compiler detected /* unknown compiler */
#endif

WiringPi's LCD Library - undefined reference to `lcdInit'

Trying to compile a very simple program for testing wiringpi's library for the hd44780 lcd controller:
#include <iostream>
#include <wiringPi.h>
#include <lcd.h>
...
int main()
{
init_wiringpi;
fd = lcdInit(2, 16, 4, 11,10 , 0,1,2,3,0,0,0,0) ;
...
return = 0;
}
Build Messages
LCD_first.cpp - undefined reference to `lcdInit'
LCD_first.cpp - undefined reference to `lcdPosition'
LCD_first.cpp - undefined reference to `lcdPuts'
Ok i just had to add lcd.o to the linked libraries list in Code::Blocks (settings/compiler/Linker settings/Link libraries >> /home/pi/wiringPi/devLib/lcd.o).