38章 C言語との連携

http://www.geocities.jp/ky_webid/cpp/language/038.html

基本的に読んで理解する系ですね。

C言語のライブラリをC++で利用可能にするための宣言「extern "C"」というものです。

一応自分でも確認してみました。

// -- main.cpp --
#include <iostream>
#include "test.h"
using namespace std;

int main () {
    
    cout << func(199) << endl;
    
    return 0;
}
// -- test.c --
#include "test.h"

int func (int num) {
    return num + 200;
}
// -- test.h --
#ifndef __TEST_H__
#define __TEST_H__

int func (int num);

#endif

これをコンパイルしてみました。

$ cl /W4 /EHs /TP main.cpp test.c
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
test.c
コードを生成中...
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj
test.obj

あれれ、普通にコンパイル通っちゃった・・・

/TPオプションつけてるからかな?

うーむ、ちょっとズッコケ気分です。

まあ今後何か問題が起きたら考えることにします。