sdl setup visual studio 2010 - c

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[])

Related

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;
}

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

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

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

readline not working properly

When I run this code in Xcode, I get build failed. I got the chunk from The Big Nerd Ranch Guide to Obj-C. I had to modify it a little (added the libraries stdlib.h and readline/readline.h) It says the build failed, but there are no errors that I can see. This question may look like a duplicate, and in a way it is, but even after seeing their solutions and trying them for myself, I still get the error.
#include <stdio.h>
#include <readline/readline.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
printf("Who is cool? ");
const char *name = readline(NULL);
printf("%s is cool!\n\n", name);
return 0;
}
Undefined symbols for architecture x86_64: "_readline", referenced
from:
_main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see
invocation)
A similar problem is mentioned in this link. You have to link the libreadline.dylib file to your project in the build phase.

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