C++ and C

This is a discussion on C++ and C within the C++ Programming forums, part of the General Programming Boards category; I was wondering can i have a .cpp file like Code: #include "gsh.h" #include <string.h> char* getStringhere(){ char * str1 ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    C++ and C

    I was wondering can i have a .cpp file like

    Code:
    #include "gsh.h"
    #include <string.h>
    
    char* getStringhere(){
    	char * str1 = "here I finished libMIDS.a";
    	return str1;
    }
    as gsh.cpp
    then a header gsh.h with just

    char* getStringhere();

    as my only line in it.

    cuz i have a .c file thats has the main and i call getStringhere(); and it doesnt work says its undefined reference but i have the header in my .c file

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It sounds like you are compiling incorrectly. You need to compile gsh.cpp into an object file, then link that to your main object file to produce an executable. What compiler are you using?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Yea basically i was wondering about that because It worked completely fine doing it in C with the.h just having one line
    char* getStringhere()
    but i was wondering since i changed the file into a .cpp file would i have to doo all this object creating situation when i figured couldnt the header file just be a funciton

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Make it
    Code:
    extern "C" const char* GetStringHere()
    {
        return "here I finished libMIDS.a";
    }
    This will ensure that C code can interface with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    You should also learn about header include guards
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21