在C程序中链接C ++函数 [英] linking C++ functions in a C program

查看:71
本文介绍了在C程序中链接C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试编译一个C ++函数,然后从

a C程序调用它。

由于谷歌是我的朋友我已经结束了直到这个链接看起来非常清楚:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html


不幸的是它不起作用。

这就是我在做的事情:


--------------- --------------------------

//main.c


#include" mylib.h"

int main(int argc,char * argv []){

foo();

返回0;

}

-------------------------- ---------------

//mylib.h


#indef __cplusplus

externC{

#endif


void foo();


#ifdef __cplusplus < br $>
}

#endif

------------------------ -------------- ---

//mylib.cc

#include< iostream>

#include" mylib.h"


using namespace std; //不知道为了什么!


void foo(){

cout<<"" Hello World!\ n";

}

---------------------------------- -------

非常感谢任何建议。


Al

Hi everyone, I''m trying to compile a C++ function and then call it from
a C program.
Since Google is my friend I''ve ended up to this link which seems very clear:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

Unfortunately it does not work.
Here is what I''m doing:

-----------------------------------------
//main.c

#include "mylib.h"

int main (int argc, char *argv[]){
foo();
return 0;
}
-----------------------------------------
//mylib.h

#indef __cplusplus
extern "C"{
#endif

void foo();

#ifdef __cplusplus
}
#endif
-----------------------------------------
//mylib.cc

#include <iostream>
#include "mylib.h"

using namespace std; // don''t know what for!

void foo(){
cout<<"Hello World!\n";
}
-----------------------------------------
Thanks a lot for any suggestion.

Al

推荐答案

abasili写道:
abasili wrote:

大家好,我正在尝试编译C ++函数,然后从
$ b $调用它ba C程序。

由于谷歌是我的朋友,我已经结束了这个链接,看起来非常好

清楚:

<一个rel =nofollowhref =http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html\"target =_ blank> http://www.parashift.com/c++ -faq-lit ... c-and-cpp.html


不幸的是它不起作用。
Hi everyone, I''m trying to compile a C++ function and then call it from
a C program.
Since Google is my friend I''ve ended up to this link which seems very
clear:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

Unfortunately it does not work.



如果你遵循所有积分就行。前两个经常被忽视。

It does if you follow all the points. The first two are often overlooked.


这就是我正在做的事情:


------ -----------------------------------

//main.c


#include" mylib.h"


int main(int argc,char * argv []){

foo();

返回0;

}
Here is what I''m doing:

-----------------------------------------
//main.c

#include "mylib.h"

int main (int argc, char *argv[]){
foo();
return 0;
}



如何构建可执行文件?


-

Ian Collins

How are you building your executable?

--
Ian Collins


Ian Collins写道:
Ian Collins wrote:

>

如果你遵循所有的观点就行了。前两个经常被忽视。
>
It does if you follow all the points. The first two are often overlooked.



这是我的Makefile

Here is my Makefile


>

你是如何建造的你的可执行文件?
>
How are you building your executable?



这是我的Makefile

------------------- ----------------------


OS = OS_LINUX

CC = gcc

CXX = g ++

AR = ar


INCMYLIB = mylib.h


INC =


Here is my Makefile
-----------------------------------------

OS = OS_LINUX
CC = gcc
CXX = g++
AR = ar

INCMYLIB = mylib.h

INC =


(INCMYLIB)


OBJMYLIB = mylib.o


OBJ =
(INCMYLIB)

OBJMYLIB = mylib.o

OBJ =


这篇关于在C程序中链接C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆