Thread: Using C++ within C implementation

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    43

    Using C++ within C implementation

    I'm required to "transfer" my c++ implementation as an addition to already existing c code. The main problem are the structures that I use in c++, and that are not recognized by c. For instance, the struct
    Code:
    struct Matrix {
            std::vector<double> mat;
            unsigned int rowNo;
            unsigned int colNo;
    };
    has a std::vector member. My c++ is largely relying on the above struct, but also on
    std::vectors. What is your suggestion on most efficient transition?
    It would be very good if I could somehow declare the structures that I use in C++, in C, and then simply copy methods.

    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    You will have to implement you own vector type in C, there is no other way without using an external library.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Thanks for the message.

    What external library should I use to facilitate the work?
    I suppose the std:: preceding vector would fit withing the library environment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You seem not to understand that C and C++ are two separate languages.

    Most C++ compilers can deal with C code.
    But, to a C compiler C++ might as well be Klingon Secret Code for all it will understand of it.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    I understand that.

    But, hauzer suggest that by importing certain library in C I might utilize my C++ code.
    Anyway, what is your suggestion?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Perhaps (and I know almost zip about C++), allocate a C array set to the same size as your vector, and transfer all contents from the vector to the array.

    Or can you just treat the vector as an array, with some restrictions?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by onako
    But, hauzer suggest that by importing certain library in C I might utilize my C++ code.
    Yeah, that library is your own C++ library, but with a C interface wrapper that is declared extern "C".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Thanks!
    Could you provide me with the minimal working example, i.e. the content of .h, .cpp and .c files that I might use as the development base.
    I tried C wrapping of C++ code example, but get the error:

    In file included from source.c:1:
    header.h:2: error: expected identifier or ‘(’ before string constant

    What might be the problem?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    #ifndef __cplusplus
    should be:
    Code:
    #ifdef __cplusplus
    Also, unless you are compiling with respect to C99, the // style comment should be replaced with a /* */ style comment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Quote Originally Posted by laserlight View Post
    Code:
    #ifndef __cplusplus
    should be:
    Code:
    #ifdef __cplusplus
    Also, unless you are compiling with respect to C99, the // style comment should be replaced with a /* */ style comment.
    Done that! Now, it complains about the actual function:

    /tmp/cccMKk1q.o: In function `main':
    source.c.text+0xa): undefined reference to `func'
    collect2: ld returned 1 exit status

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to link the object file generated by the C++ compiler to your C executable program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Given the file content as defined above, what is the exact sequence of commands I should use to make it work?


    gcc -c source.c
    gcc -c source.cpp // well, same file name
    gcc source.o source.o -o myprog
    //// the above is inappropriate

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use:
    Code:
    g++ -c source.cpp
    gcc source.c source.o -o myprog
    If you really want to compile each source file separately from linking, then use -o to specify different output filenames, e.g.,
    Code:
    gcc -c source.c -o source_c.o
    g++ -c source.cpp -o source_cpp.o
    gcc source_c.o source_cpp.o -o myprog
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Quote Originally Posted by laserlight View Post
    You could use:
    Code:
    g++ -c source.cpp
    gcc source.c source.o -o myprog
    If you really want to compile each source file separately from linking, then use -o to specify different output filenames, e.g.,
    Code:
    gcc -c source.c -o source_c.o
    g++ -c source.cpp -o source_cpp.o
    gcc source_c.o source_cpp.o -o myprog
    First option produced the following:
    source.o: In function `func':
    source.cpp.text+0xa): undefined reference to `std::cout'
    source.cpp.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    source.cpp.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
    source.cpp.text+0x1c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >:perator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
    source.o: In function `__static_initialization_and_destruction_0(int, int)':
    source.cpp.text+0x45): undefined reference to `std::ios_base::Init::Init()'
    source.cpp.text+0x4a): undefined reference to `std::ios_base::Init::~Init()'
    source.o.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, you need to link the C++ runtime too, and I am not sure how to do that. One way out, if feasible, is to write a C++ wrapper for your main function (i.e., rename your main function, and call it from a C++ source file that contains the new main function), then rely on the C++ compiler linking the C++ runtime for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  2. PLease Help with Class Implementation
    By Dilmerv in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2006, 09:36 AM
  3. C and implementation
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-04-2002, 08:00 AM
  4. help with implementation
    By NANO in forum C++ Programming
    Replies: 14
    Last Post: 04-30-2002, 01:09 PM
  5. implementation?
    By calQlaterb in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:25 AM