error LNK2019 and LNK1120 - c

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.

Related

Assigning complex values in gsl

I am trying to use GSL for complex numbers, complex vectors and complex matrices in my project. I am using VS2010 and I added the address of library in Configuration Properties>C/C++>General>Additional Include Directories. But I have a stupid problem. As far as I understood, I can not use = to assign two gsl_complex, gsl_vector_complex or gsl_matrix_complex to each other.
For vectors I have to use gsl_vector_complex_set and for matrices gsl_matrix_complex_set. But for gsl_complex, I only found GSL_SET_COMPLEX in which I should give the real and imaginary parts seperatly as 2 arguments:
GSL_SET_COMPLEX (zp, real, imaginary)
In my code I have such function:
gsl_complex cmx, cmx2;
void vector_complex_exp(gsl_vector_complex *v)
{
for (i = 0; i < v->size; i++)
{
gsl_vector_complex_set(v, i, gsl_complex_exp(gsl_vector_complex_get(v, i)));
}
}
Using this, I get following errors:
error LNK1120: 2 Unresolved external references.
error LNK2001: Unresolved external symbol "_hypot".
error LNK2001: Unresolved external symbol "_log1p".
error LNK2001: Unresolved external symbol "_log1p".
I didn't understand the reason behind these errors. But I rewrite my code like this:
void vector_complex_exp(gsl_vector_complex *v)
{
for (i = 0; i < v->size; i++)
{
cmx = gsl_vector_complex_get(v, i);
//cmx2 = gsl_complex_exp(cmx);
gsl_vector_complex_set(v, i, cmx2);
}
}
Here when the second line in for is commented, there's no error. But when I uncomment it I get the following:
error LNK1120: 2 non-resolved external references.
error LNK2001: Unresolved external symbol "_log1p".
error LNK2019: Reference to non-resolved external symbol "_hypot" in function "_gsl_complex_div".
error LNK2019: Reference to non-resolved external symbol "_log1p" in function "_gsl_complex_logabs".
I don't have any _gsl_complex_div or _gsl_complex_logabs function in my code. So I am pretty sure that the problem is with assignment here. But I can not use GSL_SET_COMPLEX here too.
Can someone help me with this? Is there really no way to assign a value to gsl_complex directly?
It would be better if you published all your code here, therefore, I immediately used this code from the lowest of the examples of the GSL. I made some small changes:
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>
int main(void)
{
double data[] = { -1.0, 1.0, -1.0, 1.0,
-8.0, 4.0, -2.0, 1.0,
27.0, 9.0, 3.0, 1.0,
64.0, 16.0, 4.0, 1.0 };
gsl_matrix_view m
= gsl_matrix_view_array(data, 4, 4);
gsl_vector_complex *eval = gsl_vector_complex_alloc(4);
gsl_matrix_complex *evec = gsl_matrix_complex_alloc(4, 4);
gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(4);
gsl_eigen_nonsymmv(&m.matrix, eval, evec, w);
gsl_eigen_nonsymmv_free(w);
gsl_eigen_nonsymmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
{
int i, j;
for (i = 0; i < 4; i++)
{
gsl_complex eval_i
= gsl_vector_complex_get(eval, i);
gsl_vector_complex_view evec_i
= gsl_matrix_complex_column(evec, i);
printf("\n eigenvalue = %g + %gi\n",
GSL_REAL(eval_i), GSL_IMAG(eval_i));
printf(" eigenvector = \n");
for (j = 0; j < 4; ++j)
{
gsl_complex z =
gsl_vector_complex_get(&evec_i.vector, j);
printf(" %g + %gi\n", GSL_REAL(z), GSL_IMAG(z));
}
}
}
gsl_vector_complex_free(eval);
gsl_matrix_complex_free(evec);
system("pause");
return 0;
}
Output of this code:
(from the red arrow and below there is a mismatch with the expected output in the GSL example)
To get my output, you need to IDE (I use Visual Studio 2015):
into "your_application" Property pages -> Configuration Properties -> VC++ Directories -> (right pane)in line Executable Directories type: C:\Users\ ...(your GSL build directory path)... \gsl\Release;$(ExecutablePath)
ibid. in line Include Directories type: C:\Users\ ...(your GSL build directory path)... \gsl;$(IncludePath)
ibid. in line Library Directories type: C:\Users\ ...(your GSL build directory path)... \gsl\Release;$(LibraryPath)
Below, in the left pane, select C/C++ -> Peprocessor -> (right pane) in line Preprocessor Defenition type: WIN32;_DEBUG;_CONSOLE;GSL_DLL;%(PreprocessorDefinitions) (I use Debug mode, created empty console application). Save settings (press "Apply" and "OK" buttons)
Copy and put into Debug directories of your application project folder gsl.dll and gslcblas.dll from C:\Users\ ...(your GSL build directory path)... \gsl\Release directory
Buld your application and run it
NOTE!: In the beginning it is best to rebuild GSL with your compiler for the target application - then the work will be guaranteed.
Good luck!

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.

Open .mat files in 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

Undefined first referenced symbol in file

I get this error and I'm not sure how to fix it. This is a project for information retrieval where i am trying to calculate tf-idf using this type (1+log(freq(t,n)))*log(N/k). freq(t,n) is the frequency of a word, t in file n and N is the number of total files, k number of files that contain the word t.
Undefined first referenced
symbol in file
log /var/tmp//ccx8E8Y1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
here is my fuction where I get the error (i have #include <math.h> in the start):
void makeTF_IDF(char** words,double** weight,char** str){
int i,j,n,f;
char nameout[1024],line[1024];
double tf,idf[1443],t;
FILE *fin;
for(i=0;i<1443;i++){
n=0;
for(j=0;j<26;j++){
strcpy(nameout,strtok(str[j],"."));
strcat(nameout,"out.txt");
fin=fopen(nameout,"r");
while(1){
if(fgets(line,1024,fin)==NULL) break;
if(strstr(line,words[i])!=NULL){
n++;
break;
}
}
fclose(fin);
}
t=26/n;
idf[i]=log(t);
}
for(i=0;i<1443;i++){
for(j=0;j<26;j++){
f=0;
strcpy(nameout,strtok(str[j],"."));
strcat(nameout,"out.txt");
fin=fopen(nameout,"r");
while(1){
if(fgets(line,1024,fin)==NULL) break;
if(strstr(line,words[i])!=NULL) f++;
}
weight[j][i]=(log(1+f))*idf[i];
fclose(fin);
}
}
}
I suppose you are working on a unix environment (and if you are trying to make an executable out of this file only, you do have a main function).
You should compile with a command like this in order to search for the math library when linking:
gcc <your_filename.c> -lm
You should have an executable named a.out in your current working directory after this command

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