天天看點

C/C++:VS2012使用C代碼

    在vs2012裡面使用c代碼

1 建立testc.h 和testc.c檔案

  vs 建立testc.c,建立cpp檔案時改字尾為c

2 檔案内容如下

testc.h

#ifndef _TESTC_H_
#define _TESTC_H_
#include <stdio.h>
#include <stdlib.h>

int mysum(int a,int b);

#endif      

testc.c

#include "testc.h"

int mysum(int a,int b){
  return a + b;
}      

3 main中調用

main.cpp

// C_Demo.cpp : 定義控制台應用程式的入口點。
//

#include "stdafx.h"

extern int throw_die( void );

extern "C" {

  #include "testc.h";

} 

int _tmain(int argc, _TCHAR* argv[])
{
  printf("c++ main \n");

  int su = mysum(3,5);

  printf("%d\n",su);

  while (true)
  {

  }
  return 0;
}      

4 結果

C/C++:VS2012使用C代碼