how to read image from particular location using MEX? - c

I have a function written in C to read an image as follows:
Image *read_Image(char *filename, int showmessages)
Now I want execute this function in MATLAB by creating a gateway function and wrapper function in MEX. I have never wrote Matlab C/Mex code. I tried my luck after going through [http://cnx.org/contents/15601bc4-3cda-4964-a7e4-5e061c8aa8b7#2/Writing-C-Functions-in-MATLAB-][1] and wrote the following code. I still need to do a lot, i am stuck in midway. Can anyone guide me???
following are the code I wrote:
#include "mex.h"
#include "CVIPtools.h"
#include "CVIPimage.h"
#include "CVIPdef.h"
#include "CVIPmap.h"
#include "limits.h"
#include "threshold.h"
#include <float.h>
#include "CVIPmatrix.h"
//Here I will write a wrapper function.
/* main gateway function*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *fnameData;
int fnameLength;
char *str;
//mxArray *smData;
int sm;
double *outArray;
int r,c,bands,n;
const mwSize *dim_array;
fnameData = prhs[0];
//smData = prhs[1];
fnameLength = mxGetN(fnameData)+1;
str = mxCalloc(fnameLength, sizeof(char));
mxGetString(fnameData, str, fnameLength);
// sm = (int)(mxGetScaler(smData));
sm = mxGetScaler(prhs[1]);
if (nrhs != 2) {
mexErrMsgIdAndTxt( "MATLAB:mxisfinite:invalidNumInputs",
"Two input arguments required.");}
if (nlhs != 1) {
mexErrMsgIdAndTxt( "MATLAB:mxisfinite:invalidNumInputs",
"One output argument required.");}
n = mxGetNumberOfElements(plhs[0]);
dim_array = mxGetDimensions(plhs[0]);
r = dim_array[0];
c = dim_array[1];
bands = dim_array[2];
if(bands==3){
plhs[0] = mxCreateNumericArray(3, dim_array, mxDOUBLE_CLASS, mxREAL);
}
else
{ plhs[0] = mxCreateDoubleMatrix(r,c,mxREAL);
bands=1;
}
outArray = mxGetData(plhs[0]);
// Here I will call wrapper function.
}
I just tried compiling this code using mex filename.c I got the following error.
mex image_readCVIP.c
Creating library D:\Users\Deepen\AppData\Local\Temp\mex_sedh1H\templib.x and object D:\Users\Deepen\AppData\Local\Temp\mex_sedh1H\templib.exp
image_readCVIP.obj : error LNK2019: unresolved external symbol mxGetScaler referenced in function mexFunction
image_readCVIP.mexw64 : fatal error LNK1120: 1 unresolved externals
D:\PROGRA~1\MATLAB\R2012A\BIN\MEX.PL: Error: Link of 'image_readCVIP.mexw64' failed
[1]: http://cnx.org/contents/15601bc4-3cda-4964-a7e4-5e061c8aa8b7#2/Writing-C-Functions-in-MATLAB-

I'm not aware of a function called mxGetScaler. Try mxGetScalar. If you really mean mxGetScaler, it must be from another library (your CVIP tools?). You will have to link any pre-compiled dependencies by appending the .lib files to the mex command, or compile their source along with the mex file by appending the source files to the command.
Note that you will have several other compilation issues with the shown code. const correctness and pointer casting errors abound.

Related

What to #include to make uriparser code compile

I am trying to build a web crawler and need to parse URIs.
I have tried to compile some simple uriparser code which I copied and pasted from https://uriparser.github.io/doc/api/latest/
#include <uriparser/Uri.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
UriUriA uri;
const char * const uriString = "file:///home/user/song.mp3";
const char * errorPos;
if (uriParseSingleUriA(&uri, uriString, &errorPos) != URI_SUCCESS) {
/* Failure (no need to call uriFreeUriMembersA) */
return 1;
}
/* Success */
uriFreeUriMembersA(&uri);
}
The code doesn't compile and gives the following errors:
/tmp/ccWkKj10.o: In function `main':
uri.c:(.text+0x51): undefined reference to `uriParseSingleUriA'
uri.c:(.text+0x6b): undefined reference to `uriFreeUriMembersA'
collect2: error: ld returned 1 exit status
I have the latest version of liburiparser-dev installed (0.9.3-2).
Any idea what I am doing wrong?
EDIT
Adding flag -luriparser solved the problem.

Using VLFeat from C

I am trying to use VLFeat in my C code on Ubuntu 16.04.3. Following the official instructions, I created a test file:
extern "C"
{
#include <vlfeat/vl/svm.h>
}
int main (int argc, const char * argv[]) {
VL_PRINT ("Hello world!") ;
return 0;
}
From the command line, I can confirm that svm.h is in vlfeat/vl. I then try
gcc main.cpp
but get the error message
main.cpp:3:27: fatal error: vlfeat/vl/svm.h: No such file or directory
Any advice on how to correctly use VLFeat in C code? Thanks.

problems compiling xmlrpc-c program

I'm trying out the examples in the xmlrpc-c documentation:
#include <stdio.h>
#include <xmlrpc.h>
#include <xmlrpc_server.h>
//#include <xmlrpc_server_abyss.h>
#include <xmlrpc_abyss.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>
static xmlrpc_value *
sample_add(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int
main (int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
I try to compile using gcc:
gcc source.c
nohting fancy and I get:
/tmp/ccfGuc6A.o: In function sample_add':
source.c:(.text+0x38): undefined reference toxmlrpc_decompose_value'
source.c:(.text+0x6d): undefined reference to xmlrpc_build_value'
/tmp/ccfGuc6A.o: In functionmain':
source.c:(.text+0x96): undefined reference to xmlrpc_env_init'
source.c:(.text+0xa5): undefined reference toxmlrpc_registry_new'
source.c:(.text+0xd8): undefined reference to xmlrpc_registry_add_method'
source.c:(.text+0x117): undefined reference toxmlrpc_server_abyss'
collect2: error: ld returned 1 exit status
these functions exist in the:
/usr/include/xmlrpc-c/base.h
whihc I have referenced:
include
I think I'm not passing the right options to link, I don't know how it's done though.
thanks
You definitely don't pass the correct argument for the linker. Just including a header file doesn't actually make the linker link with the library, you need to use the -l (lower-case L) option to tell the linker which libraries you need to link with, like
gcc source.c -lxmlrpc
I believe that xml-rpc-c comes with a helper program, intended to help you get the linking right. Its documented here
http://xmlrpc-c.sourceforge.net/doc/xmlrpc-c-config.html

CUDA linking Error undefined reference [duplicate]

This question already has answers here:
Cuda C - Linker error - undefined reference
(2 answers)
Closed 7 years ago.
I'm new to CUDA programming hence running into issues with compiling/ linking files. I'm trying to compile .c and .cu files.
Here are the files:
p3.c:
#include <stdlib.h>
#include <stdio.h>
extern void load_scheduler(int k, int j);
int blocks, threads;
int main(int argc, char* argv[])
{
if (argc > 1)
{
blocks = atoi(argv[1]);
threads = atoi(argv[2]);
}
else
exit(1);
load_scheduler(blocks, threads);
}
And scheduler.cu file:
#include <stdlib.h>
#include <stdio.h>
__global__ void sched_func()
{
int j = 6*5*threadIdx.x;
printf("%d\n",j);
}
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}
I compile these two files using nvcc -c scheduler.cu p3.c and it seems fine
However, when I try to link these two files using nvcc -o cuda_proj scheduler.o p3.o, I get an error:
p3.o: In function `main':
p3.c:(.text+0x58): undefined reference to `load_scheduler'
collect2: ld returned 1 exit status
I may not be using the right steps to get this working, so if there's any other way I should try out, suggestions are welcome. I am also new to making Makefiles so want to stick to using nvcc commands on terminal.
Just added : extern "c" before load_scheduler definition. NVCC could not recognize the function definition as it belonged to .cu file, therefore the error.
extern "C"
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}

Visual Studio C programming: Unresolved external symbol, but visible in watch window

In my main.c , i have :
int myvar[4] = {0,1,2,3};
int main(int argc, char *argv[])
{
...................
}
When I try to access myvar in another file as below:
extern int myvar[4];
if (myvar[0] == 1) do something;
When I build this (after cleaning the project), errors "LNK2001: unresolved external symbol _myvar" and "fatal error LNK1120: 1 unresolved externals" are observed. But if I comment the 'if' statement alone, then the solution builds fine and I can observe the correct values for myvar in the watch window in debug mode. I am confused regarding why this is happening. Any help would be highly appreciated!
Since you mention in your comment that the usage is split up into two different libs, you need to set up a dependency for the using library (the one that has extern int myvar[4];) to the providing library (the lib providing the main function and declaring int myvar[4]).
However, this could lead you into the problem that you have circurlar dependencies: usually the library providing the main function will call some functions in other libraries.
If you need myvar in your using library, then pass it down with an init function that gets called in order to initialize the using library.
An example for your work to be done assuming that the main() function is provided by lib_a and the library accessing myvar is called lib_b:
In lib_a do the following:
int main(int argc, char *argv[])
{
lib_b_init(sizeof(myvar)/sizeof(*myvar), myvar);
/* rest of your code goes here */
}
In lib_b in addition to the functions already provided, add the following:
static int* g_libb_myvar = (int*) 0; /* local pointer to myvar of lib_a */
static int g_libb_myvar_size = 0; /* to note the length of the array */
/* Initializer for lib_b
*
* #param a_count number of elements held in the a_myvar array
* #param a_myvar array getting passed down by caller
* */
void lib_b_init( int a_count, int* a_myvar ) {
g_libb_myvar_size = a_count;
g_libb_myvar = a_myvar; /* now, you could use g_libb_myvar in lib_b */
}

Resources