C API call from C ++ missing printf command output

Let's look at some sample code.

ctest1.c

#include<stdio.h>

void ctest1(int *i)
{
   printf("This is from ctest1\n"); // output of this is missing
   *i=15;
   return;
}

ctest2.c

#include<stdio.h>

void ctest2(int *i)
{
   printf("This is from ctest2\n"); // output of this is missing
   *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?

+4
source share
1 answer

This works well for me (OSX 10.8, LLVM 6.0).

, , printfs . r () q.

/, . ios_base:: sync_with_stdio (1), , . http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/

+4

Source: https://habr.com/ru/post/1569694/


All Articles