how to new a std c++ class in iphone programming

Discussion in 'Mac Programming' started by krautrockblog, Jan 20, 2008.

  1. krautrockblog macrumors newbie

    Joined:
    Jan 20, 2008
    #1
    i try to build a iphone game by porting from the existing c++ game source code. but the trouble is that i cannot new a std c++ class. note that i have installed the recent iphone toolchain and can compile doom source code etc correctly,the doom sample source is c, not c++. (i can build a helloworld as well,which is object c).
    i have tried with apple-darwin-gcc or apple-darwin-g++.always failed.
    anybody knew how to handle with this trouble?
     
  2. coolhome macrumors newbie

    Joined:
    Jan 29, 2008
  3. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #3
    Is this the unofficial SDK that's floating about?

    This might be a stupid question, but are you sure it supports C++? The only samples I've seen are Obj-C.
     
  4. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
  5. coolhome macrumors newbie

    Joined:
    Jan 29, 2008
    #5
    hrrrr,I have fixed the problem.
    Some suggestions are as following:
    1\ change all *.m files ext to *.mm
    2\put your c++ code in *.cpp files
    3\change your makefile .
    Code:
    CC=arm-apple-darwin-gcc
    CXX=arm-apple-darwin-g++
    LD=$(CXX)
    
    LDFLAGS=-lobjc -framework CoreFoundation -framework Foundation -framework UIKit -framework LayerKit
    LDFLAGS+=-L/usr/local/lib  -lz -lstdc++
    
    CFLAGS=-Wall -O2
    CFLAGS   +=   -I/usr/local/include 
    
    all:    hello
    
    hello:  Hello.o HelloApplication.o hello1.o
    	$(LD) $(LDFLAGS) -o $@ $^
    
    %.o: %.mm
    	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    	
    %.o: %.cpp
    	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    
    clean:
    	rm -f *.o hello
    
    4\in *.mm files ,you can new a class.


    hmmmm,if you still have questions,please send email to me.[pchard AT 126 DOT com].

    I have another questions:
    How to user BSD socket in iPhone?
    The following code is not work:
    Code:
    	NSString* hostName = [[NSString alloc] initWithString:@"www.google.com"];
    	unsigned short port = 80;
    	
      struct hostent* remoteHost;
        struct sockaddr_in remoteAddr;
    
        // Socket must be created, and not already connected
        if ( (socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
            [NSException raise:SOCKET_EX_CANT_CREATE_SOCKET 
                            format:SOCKET_EX_CANT_CREATE_SOCKET_F, strerror(errno)];
                            
        if ( socketfd == SOCKET_INVALID_DESCRIPTOR )
            [NSException raise:SOCKET_EX_BAD_SOCKET_DESCRIPTOR 
                            format:SOCKET_EX_BAD_SOCKET_DESCRIPTOR];
        connected=NO;
        if ( connected )
            [NSException raise:SOCKET_EX_ALREADY_CONNECTED 
                            format:SOCKET_EX_ALREADY_CONNECTED];
        
        // Look up host     
        [COLOR="Red"]if ( (remoteHost = gethostbyname([hostName cString])) == NULL )   return NULL,WHY?
    
    [/COLOR]
     

Share This Page