Open .mat files in C - c

I'm trying to write a C script able to open .mat files. The .mat files were written in Matlab 2015b 64-bit version. I'm using Visual Studio 2010 to compile the code. Here it is:
#include <stdio.h> /*Std library*/
#include "..\matlablib\mat.h" /*provided by MathWorks*/
int main(){
MATFile * pMF;
printf("Abrindo arquivo .mat...\n"); /*check (1)*/
pMF = matOpen("teste.mat","r");
printf("Arquivo .mat aberto.\n"); /*check (2)*/
getch();
}
As I compile this, I get the following message error:
matread.obj : error LNK2019: unresolved external symbol _matOpen
referenced in function _main
matread.exe : fatal error LNK1120: 1 unresolved externals
Have anyone had a similar problem before?
Thanks in advance,
Porto

Related

"multiple Lua VMs detected" error with c module lua 5.3

I have looked at every post pertaining the "multiple Lua VMs detected" error and none of their answers worked. I have done everything the lua.org building guide says to compile it and it still shows the error. Using visual studio 2019 causes an unresolved external symbol error and using the GCC command without -llua also causes unresolved symbols. Any ideas. I am using version lua 5.3.2 from the binary compiled with the provided make file.
this is the code I am trying to use
#include "lua.h"
#include "lauxlib.h"
#include <Windows.h>
int test_function(lua_State* L)
{
return 0;
}
static const luaL_Reg testlib[] = {
{"test_function", test_function},
{NULL, NULL}
};
__declspec(dllexport) int __cdecl luaopen_testlib(lua_State* L)
{
luaL_newlib(L, testlib);
return 1;
};
local lib, err = package.loadlib([[module.dll]], "luaopen_testlib")
if lib ~= nil then
--multiple Lua VMs detected
lib()
else
print(err)
end

Unresolved external symbol - show which line the symbol is in .c project

I'm trying to configure VS correctly to write some simple c programs.
If a function dummy() is not found, the function name is not highlight in the code, there is only a build error:
LNK2019 unresolved external symbol dummy referenced in function main
Line: 1
Is there a way to make VS intellisense work for function names with .c files?
Why compiler doesn't show the error line number (Line: 1 is not very helpful)?
Sample:
#include <stdio.h>
#include <stdlib.h>
void dummy()
{
}
int main()
{
dummyX(); //dummyX, dummy typo. Results in "unresolved external..." error
return EXIT_SUCCESS;
}

link error in gsl_complex_mul

I have started using gsl recenltly in a huge old C project. I have managed to add the libraries by adding the location in my system in Properties>C/C++>General>Additional Include Directories.
In my code, I am also including the following:
#include "gsl/gsl_matrix.h"
#include "gsl/gsl_matrix_complex_double.h"
#include "gsl/gsl_matrix_complex_float.h"
#include "gsl/gsl_matrix_complex_long_double.h"
#include "gsl/gsl_math.h"
#include "gsl/gsl_spmatrix.h"
#include "gsl/gsl_complex.h"
#include "gsl/gsl_complex_math.h"
#include "gsl/gsl_inline.h"
#include "gsl/gsl_complex.h"
I can now use most functions of gsl. but in the fowllowing function:
void vector_complex_mul_elements(gsl_vector_complex *v1, gsl_vector_complex *v2)
{
gsl_complex cpx1, cpx2, cpx3;
GSL_SET_COMPLEX(&cpx1, 0, 0);
GSL_SET_COMPLEX(&cpx2, 0, 0);
GSL_SET_COMPLEX(&cpx3, 0, 0);
if(v1->size != v2->size)
{
printf("Error: Lenght of arrays do not match.\n");
return;
}
for(i=0; i < v1->size; i++)
{
cpx1 = gsl_vector_complex_get(v1, i);
cpx2 = gsl_vector_complex_get(v2, i);
//cpx3 = gsl_complex_mul(cpx1 , cpx2);
gsl_vector_complex_set(v1, i, cpx3);
}
}
When I uncomment the line:
cpx3 = gsl_complex_mul(cpx1 , cpx2);
I get the following errors:
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK2001: Unresolved external symbol "_hypot".
Error LNK1120: 2 unresolved external references.
I have already tried writing it like:
gsl_vector_complex_set(v1, i, gsl_complex_mul(cpx1 , cpx2));
Then I get these errors:
Error LNK2019: Reference to unresolved external symbol "_log1p" in function "_gsl_complex_logabs".
Error LNK2019: Reference to unresolved external symbol "_hypot" in function "_gsl_complex_div".
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK1120: 2 unresolved external references.
Is this a only a linking problem or the way I am using it is wrong?
These (lop1p and hypot) functions are in the standard maths library. Are you including math.h and linking to it (-lm)? As per the GSL documentation.
It seems to me that you are wrong linked GSL library.
Try to rebuild GSL as a dll and relink it, as I showed you in your other post.

error LNK2019 and LNK1120

It is a known Topic, but i cant find the solution.
What i did:
createt and console Application.
Add a c source File
Add The path to my Header File to "additional include directorys"
write my code( or ctrl+c + crtl+v)
compile = error
Firste the Code:
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int main()
{
SNDFILE *sf;
SF_INFO info;
int num_channels;
int num, num_items;
int *buf;
int f,sr,c;
int i,j;
FILE *out;
/* Open the WAV file. */
info.format = 0;
sf = sf_open("file.wav",SFM_READ,&info);
if (sf == NULL)
{
printf("Failed to open the file.\n");
exit(-1);
}
/* Print some of the info, and figure out how much data to read. */
f = info.frames;
sr = info.samplerate;
c = info.channels;
printf("frames=%d\n",f);
printf("samplerate=%d\n",sr);
printf("channels=%d\n",c);
num_items = f*c;
printf("num_items=%d\n",num_items);
/* Allocate space for the data to be read, then read it. */
buf = (int *) malloc(num_items*sizeof(int));
num = sf_read_int(sf,buf,num_items);
sf_close(sf);
printf("Read %d items\n",num);
/* Write the data to filedata.out. */
out = fopen("filedata.out","w");
for (i = 0; i < num; i += c)
{
for (j = 0; j < c; ++j)
fprintf(out,"%d ",buf[i+j]);
fprintf(out,"\n");
}
fclose(out);
return 0;
}
The Error Message:
1>SoundIO.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_sf_open" in Funktion "_main".
1>SoundIO.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_sf_read_int" in Funktion "_main".
1>SoundIO.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_sf_close" in Funktion "_main".
1>C:\Users\Stephan\Desktop\BA\Audio_Coding\SoundIO\Debug\SoundIO.exe : fatal error LNK1120: 3 nicht aufgelöste Externe
The Settings: http://img5.picload.org/image/lccigod/settings.png
What i wanted to do? Simply this: http://ubuntuforums.org/showthread.php?t=968690
What i need? A way to fix this (And how to do it) or an easy guide to get this running.
The linker is telling you that you are missing definitions for the functions from libsndfile. You need to either:
Compile libsndfile from its sources, and link the resulting objects to your program.
Link an import library for libsndfile so that you can dynamically link to libsndfile.
Which solution you opt for depends on how you want to link to libsndfile.
From: http://www.cprogramming.com/tutorial/compiler_linker_errors.html
You may have issues with how you set up your compiler. For instance,
even if you include the correct header files for all of your functions,
you still need to provide your linker with the correct path to the library
that has the actual implementation. Otherwise, you will get "undefined function"
error messages...
While it looks like you've correctly included the necessary headers in your code and you've got everything compiling, you need to either statically or dynamically link to libsndfile in order to resolve your linker errors.
If you are including header files then you must ensure that they are in the include directory of your C++ directory. If it is an external file then you must add their path to your project from project properties.

error LNK2019 for ZLib sample code compiling

I created win32 console application in vs2010 (without select the option of precompiled header). And I inserted the code below. but *.obj link failed. Could you provide me more information about the error. I searched MSDN, but still can't understand it.
#include <stdio.h>
#include "zlib.h"
// Demonstration of zlib utility functions
unsigned long file_size(char *filename)
{
FILE *pFile = fopen(filename, "rb");
fseek (pFile, 0, SEEK_END);
unsigned long size = ftell(pFile);
fclose (pFile);
return size;
}
int decompress_one_file(char *infilename, char *outfilename)
{
gzFile infile = gzopen(infilename, "rb");
FILE *outfile = fopen(outfilename, "wb");
if (!infile || !outfile) return -1;
char buffer[128];
int num_read = 0;
while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
fwrite(buffer, 1, num_read, outfile);
}
gzclose(infile);
fclose(outfile);
}
int compress_one_file(char *infilename, char *outfilename)
{
FILE *infile = fopen(infilename, "rb");
gzFile outfile = gzopen(outfilename, "wb");
if (!infile || !outfile) return -1;
char inbuffer[128];
int num_read = 0;
unsigned long total_read = 0, total_wrote = 0;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);
}
fclose(infile);
gzclose(outfile);
printf("Read %ld bytes, Wrote %ld bytes, Compression factor %4.2f%%\n",
total_read, file_size(outfilename),
(1.0-file_size(outfilename)*1.0/total_read)*100.0);
}
int main(int argc, char **argv)
{
compress_one_file(argv[1],argv[2]);
decompress_one_file(argv[2],argv[3]);}
Output:
1>------ Build started: Project: zlibApp, Configuration: Debug Win32 ------
1> zlibApp.cpp
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(15): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(25): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(40): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(36): warning C4715: 'decompress_one_file' : not all control paths return a value
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(57): warning C4715: 'compress_one_file' : not all control paths return a value
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzclose referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file##YAHPAD0#Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzread referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file##YAHPAD0#Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzopen referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file##YAHPAD0#Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzwrite referenced in function "int __cdecl compress_one_file(char *,char *)" (?compress_one_file##YAHPAD0#Z)
1>D:\learning\cpp\cppVS2010\zlibApp\Debug\zlibApp.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Ah, pardon me for asking but are you actually linking in the library or object file for zlib (probably zlib1.dll if you're using an up-to-date version)?
That error is normally caused by the fact the you're missing the actual libraries with the code in it. The fact that you include the header files lets the compiler know that those functions exist but, unless you link the libraries along with your main code, the linker won't be able to find them.
Your other problems are minor. Ignore the ones suggesting that you use the so called "safe" functions. That's just Microsoft attempting some vendor lock-in and does a disservice to programmers who want to code to the standard. You can shut these warnings up by adding
#define _CRT_SECURE_NO_DEPRECATE
to the top of your source file.
The "not all control paths" warnings are because you specify your two functions to return an int but then don't actually return one. Just change those to return a void for now, you can add error checking later.

Resources