Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Koobazaur

a simple C++ program under Linux - wont compile wit gcc but will with g++...

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I''ve been playing with Linux lately and I tried to do a small program, a typical "hello world". As It turns out, it wont compile if I use other functions than main!!!
#include <stdio.h>

void zzz();

int main()
{
printf("Hello World\n");
zzz();
return 0;
}

void zzz()
{
printf("Hi\n");
}
I get the following error:
test.cpp:15:2: warning: no newline at end of file
/tmp/ccRV3g52.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0''
collect2: ld returned 1 exit status
if I quote the zzz(); line it will compile and run fine... I complie it with GCC and if I use G++ instead, it will compile fine... not that I can''t use G++, I''m just curious what''s the problem...?

Share this post


Link to post
Share on other sites
Advertisement
I''m not gonna start another thread so attatch another problem...

as I browsed through the libraries I failed to find itoa function (or equivalent) so I tried to make my own ITOA ... it compiles but doesn''t work quite as I want it to:

#include <stdio.h>

void itoa(int, char*);

main()
{
int num;
char cnum[10];

printf("Gimme a number \n");
scanf("%d", &num);
itoa(num,cnum);
printf("The number is:");
printf(" %c!", cnum);
}



void itoa(int val,char* cval)
{
printf("Transforming... \n");
cval[0]=''0'';
int y=0;

for(int x;x<val;x++)
{
y=0;
while(cval[y]==''9'')
{
cval[y]=''0'';
y++;
}
switch(cval[y])
{
case ''0'': cval[y]= ''1''; break;
case ''1'': cval[y]= ''2''; break;
case ''2'': cval[y]= ''3''; break;
case ''3'': cval[y]= ''4''; break;
case ''4'': cval[y]= ''5''; break;
case ''5'': cval[y]= ''6''; break;
case ''6'': cval[y]= ''7''; break;
case ''7'': cval[y]= ''8''; break;
case ''8'': cval[y]= ''9''; break;
}
}
printf("Done Transforming... \n");
return;
}


I get:

Gimme a number
80
Transforming...
Done Transforming...


Why wont it display the printf AFTER I called the function...? funny thing is, if I put \n in that printf just in front of the string, it will actually go to a new line in the output!!!


I didn''t know programming under linux would be that different o.0

Share this post


Link to post
Share on other sites
quote:
Original post by Koobazaur
not that I can''t use G++, I''m just curious what''s the problem...?

You named the source file with a C++ extension. gcc assumes you want to treat it as a C++ file instead of a C file, but gcc doesn''t automatically do all the "C++ things" that g++ would. Rename it to "test.c" (for example) and it''ll work.

quote:
Original post by Koobazaur
as I browsed through the libraries I failed to find itoa function (or equivalent) so I tried to make my own ITOA ...

There is no itoa function as part of the standard C runtime. Any implementations that provide it are just going out of their way. The implementations of it are horrible-by-design anyway.

I''m too lazy to check your code at the moment, so I''ll show you how to properly convert an integer to a string using a fixed size buffer and the standard C library:

#include <string.h>

/* ... */

int num = 12345;
char str[32];

snprintf(str, sizeof(str), "%d", num);

/* Being a bit over-cautious won''t hurt anything: */
str[sizeof(str)/sizeof(str[0]) - 1] = ''\0'';

You can have a dynamic buffer that isn''t oversized using C99 standard compliant snprintf implementations. Unfortunately, many standard libraries are C99 compliant at all and making such assumptions would prove to be non-portable.

Share this post


Link to post
Share on other sites
The reason it didn''t display your results is that your print format was incorrect. You specified a single character (%c), but you handed a character pointer (cnum). So, you passed a pointer and told it to treat it as a character. Not good. You should have specified %s to print the string.

Share this post


Link to post
Share on other sites
quote:
Original post by Dave Hunt
The reason it didn''t display your results is that your print format was incorrect. You specified a single character (%c), but you handed a character pointer (cnum). So, you passed a pointer and told it to treat it as a character. Not good. You should have specified %s to print the string.



oh... my... god... how did I miss THAT??? DUH!!!


Thanks for help anyway people and btw Null and Void, I don''t want you to check my code to see whther or not I convert it right, I was asking why it wont display anything...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!