CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Adding a C program to an existing c++ project

    Hi All,
    Thanks in advance for your help. I am trying to add the source code from an existing c program to my c++ project developed in Xcode. I suspect I may be over simplifying, but I was hoping I could add the source files to my project, rename the main function in the c program and call the new function from a c++ class in my existing project. The c files compile fine, but when the program tries to link I get "symbol not found". I took a look at http://www.codeguru.com/forum/showth...source+program But adding the extern "C" to the headers doesn't help. I created a simple .h and .c file to show what I am trying to do:

    temptry.h looks like:

    #ifndef MY_H_
    #define MY_H_

    #include <stdio.h>

    int my_main (int, char*);

    #endif

    and temptry.c look like:

    #include "temptry.h"

    int my_main(int argc, char *argv[]) {

    printf("here\n");
    }



    I call my_main from my c++ class which includes "temptry.h"

    the function call looks like:

    my_main(numArgs, clearcutParameters);

    Thanks again for any and all suggestions!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Adding a C program to an existing c++ project

    If you wrap the header inclusion with extern "C" in your cpp files you have to make sure the C files are compiled using the C compiler and not the C++ compiler.

    If you use MSVC check that the project properties - C/C++ - Advanced - Compile As is set to Default. If you use gcc/g++ it might do the same as default, if not there's absolutely some flag to force it.

    You might also try to get rid of the extern "C" wrapping and compile all files as C++. Surprisingly often this works just as good.

    If you still have problems post the linker errors.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Adding a C program to an existing c++ project

    Please use code tags when posting code.
    Quote Originally Posted by mothur.westcott@gmail.com View Post
    int my_main (int, char*);

    and temptry.c look like:

    #include "temptry.h"

    int my_main(int argc, char *argv[]) {
    printf("here\n");
    }
    The function declaration and definition don't match. In C++, these are two overloaded functions, but C doesn't know that concept.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Aug 2010
    Posts
    2

    Re: Adding a C program to an existing c++ project

    Thanks for the help! Adding extern "C" and changing the char* to a char** worked.

    #ifndef MY_H_
    #define MY_H_

    extern "C" {
    #include <stdio.h>

    int my_main (int, char**);
    }
    #endif

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured