I have two programs- one written in C, one in C++. I need to imbed the C++ code into the C program.
Can I do this? How?
Thanks?
Can I do this? How?
Thanks?
extern "C" int C_Wrapper_Function(char *bar)
{
return Actual_C_Plus_Plus_Function(std::string(bar));
}
#ifdef __cplusplus
extern "C" {
#endif
// your prototypes here, including shift:
void shift(float *nine, float *twentyfive);
#ifdef __cplusplus
} // to balance the opening brace above
#endif
#include "shift.h"
void shift(float *nine, float *twentyfive)
{
// implementation of shift()
}
#include "shift.h"
int main(void)
{
float nine[9];
float twentyfive[25];
shift(nine, twentyfive);
return 0;
}
int f (int x);
#ifdef __cplusplus
extern "C" {
#endif
int f (int x);
#ifdef __cplusplus
}
#endif
#ifndef CPPLib_CPPClass_h
#define CPPLib_CPPClass_h
#ifdef __cplusplus
class CPPClass{
public:
static void StatPlusPlus(int* aNum);
void PlusPlus(int* aNum);
};
# else
//This is very important! Also seen as extern struct <identifier> <identifier>
struct CPPClass;
#endif
//C API
#ifdef __cplusplus
extern "C" {
#endif
/* ANSI C prototypes */
#if defined(__STDC__) || defined(__cplusplus)
extern void c_StatPlusPlus(int*);
extern void c_PlusPlus(struct CPPClass*, int*);
#endif
#ifdef __cplusplus
}
#endif
//C and C++ friendly
#ifdef __cplusplus
extern "C" {
#endif
void GlobalPlusPlus(int* aNum);
#ifdef __cplusplus
}
#endif
#endif
//
// File.cpp
// CPPLib
//
// Created by Max on 1/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include "CPPClass.h"
void CPPClass::StatPlusPlus(int* aNum){
(*aNum)++;
}
void CPPClass::PlusPlus(int* aNum){
(*aNum)++;
}
/* ANSI C prototypes */
void c_StatPlusPlus(int* Var)
{
CPPClass::StatPlusPlus(Var);
}
void c_PlusPlus(struct CPPClass* Target, int* Var)
{
Target->PlusPlus(Var);
}
void GlobalPlusPlus(int* aNum){
(*aNum)++;
}
#include <stdio.h>
#include "CPPClass.h"
int main (int argc, const char * argv[])
{
//ported from my CPP Test
int TestInt = 10;
//Static
c_StatPlusPlus(&TestInt);
printf("%d \n", TestInt);
//Class
//Important! Must be a pointer to CPPClass
//CPPClass is an incomplete specification and the C compiler won't know what to do with it.
//Thankfully it always knows what to do with a pointer!
struct CPPClass* CP;
c_PlusPlus(CP, &TestInt);
printf("%d \n", TestInt);
//Global
GlobalPlusPlus(&TestInt);
printf("%d \n", TestInt);
printf("Hello, World!\n");
return 0;
}
I learned Tuesday that C++ was an invention from the evil Microsoft Empire.
PS. I learned Tuesday that C++ was an invention from the evil Microsoft Empire.
I learned Tuesday that C++ was an invention from the evil Microsoft Empire.
C++ is designed by Microsoft? No. I originally designed and implemented C++ and together with the ISO C++ standards committee refined its definition. Microsoft has taken an active and largely positive role in this standardization - as has Apple, GNU, IBM, Sun, and many others. Like others - e.g. Apple - Microsoft tries to help/lock-in their users with proprietary extensions, (in particular, Microsoft C++/CLI).
You'll have to correct me again if I'm misreading your posts, but I don't believe you can expose any function that uses IOStream or any of the other C++ STL headers in C, even when obfuscating them through a library with a C API.
double f(int i)
{
std::vector<double> values(some_cpp_function());
return values[i];
}
I stand corrected. Never having found a use for OOP, anything I can blame on Bill Gates, I can. The time wasted dealing with windows and windows machines has left me with a permanent dislike for the man, the company and its products.
Yes you can, as long as the function prototype doesn't contain any C++ types. For example, it's perfectly fine to have
don't mind the crappy code itself; I just wanted to point out that in the implementation of the function, you're free to use as much C++ as you like. You will still be able to call this function from C.Code:double f(int i) { std::vector<double> values(some_cpp_function()); return values[i]; }
void foo(std::string bar)
{
cppFoo(bar);
}