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.
Related
The file handling commands in Visual Studio seem to be different than normal. I'm currently learning the very basics of File Handling in C, but the commands don't seem to be working. This is what I've got right now -
#include <stdio.h>
int main()
{
int num;
FILE *fptr;
fptr = fopen("C:\\", "program.txt", "w");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf_s("%d", &num);
fprintf(fptr, "%d", num);
fclose(fptr);
return 0;
}
Here's the build output-
'fopen': too many actual parameters
warning C4013: 'exit' undefined; assuming extern returning int
error 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.
When I use fopen_s instead, like this fopen_s("C:\program.txt", "w"), it says-
'function': 'FILE **' differs in levels of indirection from 'char [15]'
'fopen_s': different types for formal and actual parameter 1
'fopen_s': too few arguments for call
'=': 'FILE *' differs in levels of indirection from 'errno_t'
I need some serious help.
You should open your file with either
FILE * f;
f= fopen("C:\\program.txt", "w");
or
FILE * f;
int err = fopen_s(&f, "C:\\program.txt", "w");
the latter takes FILE ** as an extra argument, and return error code (0 on success).
There is a extra comma , in fopen() which makes fopen() as three arguments, which is wrong & causing the error
'fopen': too many actual parameters
This
fptr = fopen("C:\\", "program.txt", "w"); /* fopen() expects 2 arguments */
replaces with
fptr = fopen("C:\\program.txt", "w");
You can disable below
'fopen': This function or variable may be unsafe. Consider using
fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
warning by
#pragma warning(disable:4996) /* use it before other headers */
Or use fopen_s().
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
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.
I am trying to read text in from a file and insert that text into a text box.
This is the code I am using.
FILE *infile;
GdkFont *fixed_font;
infile = fopen("text.txt", "r");
fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-140-*-*-*-*-*-*");
if (infile) {
char buffer[1024];
int nchars;
while (1)
{
nchars = fread(buffer, 1, 1024, infile);
gtk_text_insert(view, fixed_font, NULL, NULL, buffer, nchars);
if (nchars < 1024)
break;
}
fclose (infile);
These are my includes
#include <gtk/gtk.h>
#include <gtk/gtktext.h>
When I compile I get this warning:
warning: implicit declaration of function ‘gtk_text_insert’
I have read on this forum and others that implicit declaration errors come from using functions before they are declared. However, gtk_text_insert() is included in the header file so how can this be implicit?
I am using the following software:
gtk version 2.20.1
Red Hat Enterprise Linux Server release 6.5
GtkText is deprecated and unsupported. It is known to be buggy. To use it, you must define the symbol GTK_ENABLE_BROKEN prior to including the GTK+ header files. Use GtkTextView instead.
From https://developer.gnome.org/gtk2/stable/GtkText.html
I am working on a C project where I need to get the list of files that are within a directory. I am using dirent.h but am having some problems getting it to work, I am building the program under Linux.
When I try and build the program I get the following error
myClass:error: âDIRâ undeclared (first use in this function)
myClass:408: error: (Each undeclared identifier is reported only once
myClass:408: error: for each function it appears in.)
myClass:408: error: âdirâ undeclared (first use in this function)
myClass:410: warning: implicit declaration of function âopendirâ
myClass:413: warning: implicit declaration of function âreaddirâ
myClass:413: warning: assignment makes pointer from integer without a cast
myClass:415: error: dereferencing pointer to incomplete type
myClass:417: warning: implicit declaration of function âclosedirâ
Below is the code that I am using
int logMaintenance(void *arg)
{
DIR *dir;
struct dirent *ent;
dir = opendir(directory);
if (dir != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
printf("%s\n", ent->d_name);
}
closedir(dir);
}
else
{
printf("Failed to read directory %i", EXIT_FAILURE);
}
return 0;
}
I don't understand what these errors mean especially when it says that DIR is undeclared when I have included the dirent.h header file for Liunux.
Thanks for your help.
You should make sure that:
You #include <dirent.h>, rather than "dirent.h", so that the system search path for headers is used to locate that file
You don't have a dirent.h file lying around somewhere in your project that could be picked up instead.
When trying to debug this type of strange problem, ask GCC for the pre-processed output with gcc -E. You can see what files (including the paths) it's including. That can help a lot.
And if you're using Microsoft Visual Studio, head over to this question:
Microsoft Visual Studio: opendir() and readdir(), how?
I'm not sure, but it seems like I was always told that you always need a main function...
However I have only a mere 8 months (2 semesters) of C++ under my belt. I just practice it to be safe, however, I would also use:
int main(int argc, char **argv) or
int main(int argc, char *argv[]) rather than
int logMaintenance(void *arg)
(while using dirent.h).