Linker 2019 error while compiling sqlite example code with cl.exe - c

I know this is some kind of linking error, and is likely due to me missing some kind of compiler option. So please let me know what it is I'm doing wrong.
My code comes from the sample program # http://www.sqlite.org/quickstart.html
#include <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 ){
fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
return(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
I am using the "Developer Command Prompt" in Windows with the following command to compile: (the SQLite header file is in %USERPROFILE%\lib)
cl /I%USERPROFILE%\lib helloworld.c
and I get the following error message:
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
helloworld.c
Microsoft (R) Incremental Linker Version 14.00.23918.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:helloworld.exe
helloworld.obj
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_close referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_exec referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_free referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_open referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_errmsg referenced in function _main
helloworld.exe : fatal error LNK1120: 5 unresolved externals

Add the sqlite library file on the command line, like this:
cl /I%USERPROFILE%\lib helloworld.c sqlite3.lib
EDIT: It appears they provide a .def file in their precompiled binaries, and no .lib. To create the .lib:
lib /def:sqlite3.def /OUT:sqlite3.lib

Related

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

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.

sdl setup visual studio 2010

I can not get sdl up and running. I included the include folder, the library folder
and here is the code
#include <SDL.h>
int main()
{
if (SDL_Init(SDL_INIT_VIDEO)==-1)
{
return 1;
}
SDL_Quit();
return 0;
}
what am i doing wrong?
It gives me this error message
Error 1 error LNK2019: unresolved external symbol _SDL_main referenced in function _main c:\Users\user1\documents\visual studio 2010\Projects\Sdl Test 1\Sdl Test 1\SDLmain.lib(SDL_win32_main.obj) Sdl Test 1
Error 2 error LNK1120: 1 unresolved externals c:\users\user1\documents\visual studio 2010\Projects\Sdl Test 1\Debug\Sdl Test 1.exe 1 1 Sdl Test 1
EDIT: I got it working It is because I did not use this
int main(int argc, char* argv[])
EDIT: I got it working It is because I did not use this
int main(int argc, char* argv[])

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