Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Compiling a C based mud under g++

Compiling a C based mud under g++

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Crix   (11 posts)  Bio
Date Tue 01 Aug 2006 12:44 PM (UTC)
Message
Greetings everyone,
At the suggestion of Odis, I'm posting in hoping for suggestions. I currently run a copy of SWR that I am trying to compile under g++ in the hopes of eventually being able to use many of the features of C++. I've been able to clean out all the errors in my compiles as well as declaring extern "C" around my mud.h file, but when it gets to linking it all together, I get pages and pages of undefined reference errors about functions that are prototyped in mud.h, defined in hashstr.c and used in a macro in mud.h. An example of one of the functions was str_alloc.
After begging and pleading for help from Odis(hehe, thanks a bunch for what you did), he tried renaming the function just in case there was an overloading issue, but that didn't appear to work. We also moved the prototype declarations above the macro's in case that was it, but there was no change.
I'm at a loss as to what it is because the code appears to compile all fine and such but snags itself while linking.
Thanks in advance,
-C
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 01 Aug 2006 01:04 PM (UTC)
Message
Hmm. Are you also using g++ for linking? It sounds like your problem is during the linking phase and changing the order of functions/prototypes won't help you.

It's also possible that you removed an object file from the link phase; did you tweak the makefile, or move stuff from one file to a new one? And did you try make clean, make all?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Crix   (11 posts)  Bio
Date Reply #2 on Tue 01 Aug 2006 01:08 PM (UTC)
Message
Lemme apologize first, I think I put this in the wrong section.

The only section that I changed in the makefile was changing gcc to g++
Other than that, I only repaired bugs and never actually touched the errored functions.

Top

Posted by Kiasyn Kelle   (15 posts)  Bio
Date Reply #3 on Tue 01 Aug 2006 01:17 PM (UTC)
Message
i never extern "C"'d my mud.h file when i converted.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #4 on Tue 01 Aug 2006 01:25 PM (UTC)
Message
Oh, I didn't notice that you'd used extern "C". You don't want to do that.

You only use extern "C" { ... } when the externed functions were compiled in a C (not C++) object file, and therefore use the C calling convention, instead of C++.

So if your functions are compiled using g++, you will in fact break things if you tell the compiler to expect C versions, because you have compiled C++ versions.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Crix   (11 posts)  Bio
Date Reply #5 on Tue 01 Aug 2006 01:32 PM (UTC)
Message
The problem was much the same though without that.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #6 on Tue 01 Aug 2006 02:07 PM (UTC)
Message
Well, nonetheless, you shouldn't be using extern "C". Did you do make-clean/make-all in between, after modifying mud.h and removing extern "C"?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Crix   (11 posts)  Bio
Date Reply #7 on Tue 01 Aug 2006 02:41 PM (UTC)
Message
Yup, I was always taught if you modify mud.h that you need to make clean/make. When I get back to work on it tonight, I'll remove though I'm almost certain most, if not all of the problems will still be there.
Top

Posted by Nick Gammon   Australia  (23,162 posts)  Bio   Forum Administrator
Date Reply #8 on Tue 01 Aug 2006 11:52 PM (UTC)
Message
I tackled this project a while back with some success, the versions would be old now:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=3668

As for your link errors, I'm not sure what is happening exactly. Did you rename all your .c files as .cpp ones?

I think I would skip the "extern C" stuff, and try to make the whole lot compile and link as straight C++. I know it can be done, I think that is what I did in my earlier project.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Samson   USA  (683 posts)  Bio
Date Reply #9 on Wed 02 Aug 2006 04:09 AM (UTC)
Message
A small sampling of some of the linker errors you're getting might help too. Those of us who have been through the g++ conversion will probably be able to tell right away what's going on.


A question of my own though Nick. Is there any benefit in renaming the files as .cpp instead of leaving them as .c ?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #10 on Wed 02 Aug 2006 04:27 AM (UTC)
Message
Quote:
Is there any benefit in renaming the files as .cpp instead of leaving them as .c ?
Yes -- it can help compilers auto-detect the right language. g++ can drop into C mode if the files have the .c extension, and that can cause some weirdness. Usually it figures it out, but it's safer, and besides, it's just good programming practice in general.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,162 posts)  Bio   Forum Administrator
Date Reply #11 on Wed 02 Aug 2006 06:08 AM (UTC)
Message
Quote:

Is there any benefit in renaming the files as .cpp ...


I agree with Ksilyan that the compiler is more likely to treat your source as C++ files, rather than C being compiled in C++ mode which gives a rather different result. Of course there will be a compiler switch to do that. Judging by the (incredibly long) man page for g++ it is:

-x c++

However a quick test seems to show that compiling a .c file with g++ does in fact treat it as C++. However as Ksilyan says, if they are C++ files, why not have them suffixed .cpp?

This doesn't totally explain your linking problems, however I would, as I said before, stop using "extern C" and make the source compile without it, as far as possible anyway.

It will probably be needed for external libraries (zlib springs to mind), but should not be needed for things like including mud.h.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,162 posts)  Bio   Forum Administrator
Date Reply #12 on Wed 02 Aug 2006 06:21 AM (UTC)
Message
As an experiment, I took the SmaugFUSS source (1.6 I think) and changed gcc in the Makefile to g++, did a "make clean" and then a make.

This compiled and linked with no errors whatsoever.

I wasn't sure what that proved, so I added a bit of definite C++ to inside comm.c :


vector<int> v (100);
generate (v.begin (), v.end (), rand);


That compiled OK too, so it seems that it should be working for you with no errors.

Thanks to Samson for changing "class" to "Class" which avoided heaps of problems I had earlier.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,162 posts)  Bio   Forum Administrator
Date Reply #13 on Wed 02 Aug 2006 06:24 AM (UTC)
Message
zlib actually has the extern "C" inside zlib.h, so you don't need to do that either.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Crix   (11 posts)  Bio
Date Reply #14 on Wed 02 Aug 2006 12:03 PM (UTC)
Message
Excellent, thank you everyone for the work, when I get home tonight, I'll try it it out and copy some of the errors that I have.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


72,496 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.