Thread: [Dev-C++] The Winmain function
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jan M. <jan...@vo...> - 2008-03-30 01:49:58
|
Hello,
maybe it is clear but I am a little bit confused.
This is the source code:
#include<stdio.h>
#include<string.h>
const char *module_name = "conf_proces.c";
const char *conf_file_name = "..\\conf\\conf.xml";
int conf_file_read(const char *in_conf_file_name)
{
const char *function_name = "conf_file_read";
const char *err_msg;
int i;
FILE *p_fo;
/*open and read the conf file*/
if ((p_fo = fopen (conf_file_name, "r")) == NULL)
{
/* ERROR PROCESSING */
err_msg = strcat ("Cannot open the configuration file: ", strcat
(in_conf_file_name, "\n"));
/* write_err (module_name, function_name, err_msg); */
}
if ( (i = (fclose (p_fo) != 0)))
{
/* ERROR PROCESSING */
err_msg = strcat ("Cannot close the configuration file: ", strcat
(in_conf_file_name, "\n"));
/* write_err (module_name, function_name, err_msg); */
}
return 0;
}
And this is the gcc outpput:
C:\Documents and Settings\Jan Mura\_myhome\dev\dialogues\src>gcc
conf_process.c
conf_process.c: In function `conf_file_read':
conf_process.c:27: warning: passing arg 1 of `strcat' discards qualifiers
from p
ointer target type
conf_process.c:36: warning: passing arg 1 of `strcat' discards qualifiers
from p
ointer target type
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x10
6):main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
It is ok with these warnings, I understand it.
But I don't understand what means the error message about the WinMain
function. There is no main neither WinMain. I don't use windows.h, Win
components or anything like that.
Jan Mura
jan...@vo...
|
|
From: Michal M. <mi...@mo...> - 2008-03-30 04:45:38
|
Jan Mura napsal(a): > function. There is no main neither WinMain. I don't use windows.h, Win > components or anything like that. If there is no main or WinMain you can only compile it (Ctrl+F9) but not run. |
|
From: Per W. <pw...@ia...> - 2008-03-30 07:15:08
|
No, I do not think you understand the other warnings.
You should not treat them as warnings, since in reality they are not
warnings but errors. You have completely misunderstood the strcat()
function.
It's prototype is:
char *strcat(char *dest, const char *src);
Notice that the second parameter has the 'const' keyword, but not the
first. That is absolutely vital! Why? Because the first parameter is a
target buffer that will be _modified_ by the call! The second parameter is
const because it will only be read.
Anything pointer or buffer you use as first parameter must have write
access and have enough room to hold the resulting string.
What do you think happens when you do:
err_msg = strcat ("Cannot open the configuration file: ",
strcat(in_conf_file_name, "\n"));
Hint:
Your file name in in_conf_file_name will be modified to also contain a
"\n".
But in_conf_file_name lies in memory you do not own, and hence are not
allowed to write to.
Also, the text string "Cannot open the configuration file: " will also be
used as a destination buffer, and will have the file name and a "\n"
appended to it. But where is the room for that extra text? And MinGW will
place the string literal in read-only memory, so your program will be
terminated!
strcat() is not a function that magically glues together two strings and
returns a pointer to some internal magic result.
It is a function that takes a first argument and extends it with a second
string. After that, it will return a pointer to this first (now already
modified) argument.
So, you can use strcat() as:
char buf[100];
char *res;
strcpy(buf,"Hello");
strcat(buf," World!");
printf("%s\n",buf);
or you can write:
strcpy(buf,"Hello");
printf("%s\n",strcat(buf," World!"));
or you can write:
printf("%s\n",strcat(strcpy(buf,"Hello")," World!"));
or you can write:
res = strcat(strcpy(buf,"Hello")," World!");
printf("%s\n",res);
but you can not write:
res = strcat("Hello"," World!");
But in the end, why not just write:
char error_buf[500];
snprintf(error_buf,sizeof(error_buf),"Can not open the configuration file
%s",conf_file_name);
strcat is used for glueing together strings.
s(n)printf is used for creating formatted output.
You are not interested in concatenating any strings...
/pwm
On Sun, 30 Mar 2008, Jan Mura wrote:
> Hello,
>
> maybe it is clear but I am a little bit confused.
>
> This is the source code:
>
> #include<stdio.h>
> #include<string.h>
>
> const char *module_name = "conf_proces.c";
>
> const char *conf_file_name = "..\\conf\\conf.xml";
>
> int conf_file_read(const char *in_conf_file_name)
> {
> const char *function_name = "conf_file_read";
> const char *err_msg;
> int i;
> FILE *p_fo;
>
> /*open and read the conf file*/
> if ((p_fo = fopen (conf_file_name, "r")) == NULL)
> {
> /* ERROR PROCESSING */
> err_msg = strcat ("Cannot open the configuration file: ", strcat
> (in_conf_file_name, "\n"));
> /* write_err (module_name, function_name, err_msg); */
> }
>
>
> if ( (i = (fclose (p_fo) != 0)))
> {
> /* ERROR PROCESSING */
> err_msg = strcat ("Cannot close the configuration file: ", strcat
> (in_conf_file_name, "\n"));
> /* write_err (module_name, function_name, err_msg); */
> }
> return 0;
> }
>
> And this is the gcc outpput:
> C:\Documents and Settings\Jan Mura\_myhome\dev\dialogues\src>gcc
> conf_process.c
> conf_process.c: In function `conf_file_read':
> conf_process.c:27: warning: passing arg 1 of `strcat' discards qualifiers
> from p
> ointer target type
> conf_process.c:36: warning: passing arg 1 of `strcat' discards qualifiers
> from p
> ointer target type
> C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x10
> 6):main.c: undefined reference to `WinMain@16'
> collect2: ld returned 1 exit status
>
> It is ok with these warnings, I understand it.
> But I don't understand what means the error message about the WinMain
> function. There is no main neither WinMain. I don't use windows.h, Win
> components or anything like that.
>
> Jan Mura
> jan...@vo...
|
|
From: Pink B. <da...@ya...> - 2008-03-30 17:16:33
|
Per Westermark sez, > No, I do not think you understand the other warnings. > > You should not treat them as warnings, since in reality > they are not warnings but errors. You have completely > misunderstood the strcat() function. Um... it's not that its just a misunderstanding of what a string is in C. In a lot of languages, strings are really strings, with a defined length, and helpful built in stuff to allocate memory for them etc etc. In C a string is nothing more than an static array of char. Hopefully with a NULL char marking the end of the string. Thats hopefully because if it's not there then really bad things happen because C doesn't have bounds checking on arrays. Which means it's very easy to do things like copy data past the end of a string, thus corrupting memory. PS: When I get a warning I always look to see what is causing it and then do the appropriate cast or what not to make it go away. Mr Foo |
|
From: hhh h. <the...@ho...> - 2008-03-30 16:57:08
|
Jan : Hi there :D I'll assume you've read the excellent response by Per Westermark, so your prob is WinMain@16 ? well, this actually is a Linker error... the linker is complaining because PE files do contain, like every other executable formats an Entry Point. This, in C, are main our WinMain functions ( the latter in case, of win32 executables obviously. ) now if you do not provide a entry point for your program then there is no way the linker can calculate the offset of your functions,Data, and ultimatly instructions from the starting address, the latter being *roughfly* the entry point(main/WinMain)... :D Get it ? > From: jan...@vo...> To: dev...@li...> Date: Sun, 30 Mar 2008 03:49:35 +0200> Subject: [Dev-C++] The Winmain function> > Hello,> > maybe it is clear but I am a little bit confused.> > This is the source code:> > #include<stdio.h>> #include<string.h>> > const char *module_name = "conf_proces.c";> > const char *conf_file_name = "..\\conf\\conf.xml";> > int conf_file_read(const char *in_conf_file_name)> {> const char *function_name = "conf_file_read";> const char *err_msg;> int i;> FILE *p_fo;> > /*open and read the conf file*/> if ((p_fo = fopen (conf_file_name, "r")) == NULL)> {> /* ERROR PROCESSING */> err_msg = strcat ("Cannot open the configuration file: ", strcat> (in_conf_file_name, "\n"));> /* write_err (module_name, function_name, err_msg); */> }> > > if ( (i = (fclose (p_fo) != 0)))> {> /* ERROR PROCESSING */> err_msg = strcat ("Cannot close the configuration file: ", strcat> (in_conf_file_name, "\n"));> /* write_err (module_name, function_name, err_msg); */> }> return 0;> }> > And this is the gcc outpput:> C:\Documents and Settings\Jan Mura\_myhome\dev\dialogues\src>gcc > conf_process.c> conf_process.c: In function `conf_file_read':> conf_process.c:27: warning: passing arg 1 of `strcat' discards qualifiers > from p> ointer target type> conf_process.c:36: warning: passing arg 1 of `strcat' discards qualifiers > from p> ointer target type> C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x10> 6):main.c: undefined reference to `WinMain@16'> collect2: ld returned 1 exit status> > It is ok with these warnings, I understand it.> But I don't understand what means the error message about the WinMain > function. There is no main neither WinMain. I don't use windows.h, Win > components or anything like that.> > Jan Mura> jan...@vo...> > > > -------------------------------------------------------------------------> Check out the new SourceForge.net Marketplace.> It's the best place to buy or sell services for> just about anything Open Source.> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace> _______________________________________________> Dev-cpp-users mailing list> Dev...@li...> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Instale a Barra de Ferramentas com Desktop Search e ganhe EMOTICONS para o Messenger! É GRÁTIS!
http://www.msn.com.br/emoticonpack |