Thread: C++ newbie question

  1. #1
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19

    Question C++ newbie question

    Here my question.I want to know what headers would be good to use in C++ if i want to use this headers i seen in a sourcecode i been reading?Or can i use C and C++ in the same program?But not sure how to do sockets in c++ and how can i do that in C++ same as C?

    #include <unistd.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <poll.h>
    #include <features.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/file.h>
    #include <errno.h>
    #include <ctype.h>
    Fedora 9 gcc 4.3

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your question is not entirely simple to answer generically, because whatever generic answer is given, it would have dozens of "except if".

    However, if we consider only header files in <> [where <> is used to indicate system/compiler delivered header files], then using those functions from C++ SHOULD be fine.

    As you may know, C++ is a superset of C, so to a large extent, C++ will work just like C, but also has "more" to offer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19
    Can i use <sys/socket.h> and <socket.h> in C++ or is that for C?
    Fedora 9 gcc 4.3

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Unless your platform is so archaic that it doesn't provide C++-compatibility in its C headers, you can use them in C++. There are very few C libraries you can't use in C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19
    Then how do i get sockets to work in C++ and what headers will not work with C++?
    Last edited by Whiteghost; 07-19-2008 at 05:05 AM.
    Fedora 9 gcc 4.3

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by Whiteghost View Post
    Then how do i get sockets to work in C++ and what headers will not work with C++?
    By including those headers you listed...?

  7. #7
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19
    i didn't know socket.h works with C++
    Fedora 9 gcc 4.3

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Whiteghost View Post
    i didn't know socket.h works with C++
    Are you eventually going to try and compile a program with it, or just wait until someone absolutely convinces you beyond a shadow of a doubt? You could have found this out yourself in about three minutes.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Whiteghost View Post
    can i use C and C++ in the same program?
    Yes you can. But: to link to a function written in C, you have to declare it as bing a C function, using either of the following methods. Headers that come with your compiler will do this for you.

    Code:
    extern "C" {
    
        int cfunc();
    }
    
    extern "C" int cfunc();
    If you are trying to use C headers that are not written to be linked with c++, you may need to wrap the #include in an extern "C" block.

    The best thing about this though is that if it links, it works. The only thing to what out for is passing arguments incompatible with C to a C function, such as non-POD objects or some C++ functions (as pointers).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    geek Whiteghost's Avatar
    Join Date
    Aug 2005
    Posts
    19
    Thanks King Mir i will try that out.
    Fedora 9 gcc 4.3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM