Let's look at some sample code.
ctest1.c
#include<stdio.h>
void ctest1(int *i)
{
printf("This is from ctest1\n");
*i=15;
return;
}
ctest2.c
#include<stdio.h>
void ctest2(int *i)
{
printf("This is from ctest2\n");
*i=100;
return;
}
ctest.h
void ctest1(int *);
void ctest2(int *);
Now let's make a c-library from
gcc -Wall -c ctest1.c ctest2.c
ar -cvq libctest.a ctest1.o ctest2.o
Now let's create a cpp-based file that will use this c apis prog.cpp
#include <iostream>
extern "C" {
#include"ctest.h"
}
using namespace std;
int main()
{
int x;
ctest1(&x);
std::cout << "Value is" << x;
ctest2(&x);
std::cout << "Value is" << x;
}
Now let's compile this C ++ program with the C library
g++ prog.cpp libctest.a
Now run it like
./a.out
Output:
Value is5Value is100
But here the meanings are true. This means that they are correctly named c apis. But the output of these printf commands is missing.
What am I missing?
source
share