免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4504 | 回复: 3
打印 上一主题 下一主题

[C++] 请教C++和C混合编程问题 [复制链接]

论坛徽章:
1
双鱼座
日期:2014-12-24 17:57:21
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-24 17:46 |只看该作者 |倒序浏览
本帖最后由 石下醉客 于 2014-12-24 17:59 编辑

有三个文件,使用了大数库GMP
test.h
  1. #include <gmp.h>
  2. #include <stdio.h>
  3. void print_msg(mpz_t message);
复制代码
test.c
  1. #include "test.h"
  2. #include <stdio.h>

  3. void print_msg(mpz_t message)
  4. {
  5.         mpz_out_str(stderr, 10, message);
  6.         fputs("\n", stderr);
  7. }
复制代码
main.cpp
  1. #include "test.h"
  2. #include <stdio.h>

  3. int main()
  4. {
  5.         mpz_t message;
  6.         mpz_init(message);
  7.         mpz_set_ui(message, 100)
  8.         print_msg(message);
  9.         return 0;
  10. }
复制代码
Makefile
  1. CC = g++
  2. CFLAGS = -Wall -I/usr/local/include/
  3. LDFLAGS = -lgmp

  4. all: main

  5. test.o: test.c
  6.         $(CC) -c -g -o $@ $^ $(CFLAGS)

  7. main.o: main.cpp
  8.         $(CC) -c -g -o $@ $^ $(CFLAGS)

  9. main: test.o main.o
  10.         $(CC) -o $@ $^ $(LDFLAGS)

  11. clean:
  12.         rm -f *.o test main
复制代码
在C++中调用C文件。这样会出现链接失败,也就是"unreference to print_msg”,于是把main.cpp改成
  1. extern "C"
  2. {
  3. #include "test.h"
  4. }
  5. #include <stdio.h>

  6. int main()
  7. {
  8.         mpz_t message;
  9.         mpz_init(message);
  10.         mpz_set_ui(message, 100L);
  11.         print_msg(message);

  12.         return 0;
  13. }
复制代码
但是编译会出现如下错误
g++ -c -g -o test.o test.c -Wall -I/usr/local/include/
g++ -c -g -o main.o main.cpp -Wall -I/usr/local/include/  
In file included from /usr/include/c++/4.8/bits/stringfwd.h:40:0,
                 from /usr/include/c++/4.8/iosfwd:39,
                 from /usr/local/include/gmp.h:34,
                 from test.h:1,
                 from main.cpp:3:
/usr/include/c++/4.8/bits/memoryfwd.h:63:3: error: template with C linkage
   template<typename>
   ^
/usr/include/c++/4.8/bits/memoryfwd.h:66:3: error: template specialization with C linkage
   template<>
   ^
/usr/include/c++/4.8/bits/memoryfwd.h:70:3: error: template with C linkage
   template<typename, typename>
   ^
In file included from /usr/include/c++/4.8/iosfwd:39:0,
                 from /usr/local/include/gmp.h:34,
                 from test.h:1,
                 from main.cpp:3:
/usr/include/c++/4.8/bits/stringfwd.h:52:3: error: template with C linkage
   template<class _CharT>
   ^
/usr/include/c++/4.8/bits/stringfwd.h:55:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT>,
   ^
/usr/include/c++/4.8/bits/stringfwd.h:59:3: error: template specialization with C linkage
   template<> struct char_traits<char>;
   ^
/usr/include/c++/4.8/bits/stringfwd.h:65:3: error: template specialization with C linkage
   template<> struct char_traits<wchar_t>;
   ^
In file included from /usr/include/c++/4.8/iosfwd:40:0,
                 from /usr/local/include/gmp.h:34,
                 from test.h:1,
                 from main.cpp:3:
/usr/include/c++/4.8/bits/postypes.h:111:3: error: template with C linkage
   template<typename _StateT>
   ^
/usr/include/c++/4.8/bits/postypes.h:214:3: error: template with C linkage
   template<typename _StateT>
   ^
/usr/include/c++/4.8/bits/postypes.h:219:3: error: template with C linkage
   template<typename _StateT>
   ^
In file included from /usr/local/include/gmp.h:34:0,
                 from test.h:1,
                 from main.cpp:3:
/usr/include/c++/4.8/iosfwd:76:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:79:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:82:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:85:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:88:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:91:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT>,
   ^
/usr/include/c++/4.8/iosfwd:95:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT>,
   ^
/usr/include/c++/4.8/iosfwd:99:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT>,
   ^
/usr/include/c++/4.8/iosfwd:103:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT>,
   ^
/usr/include/c++/4.8/iosfwd:107:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:110:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:113:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:116:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:119:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
/usr/include/c++/4.8/iosfwd:122:3: error: template with C linkage
   template<typename _CharT, typename _Traits = char_traits<_CharT> >
   ^
In file included from test.h:1:0,
                 from main.cpp:3:
/usr/local/include/gmp.h:2251:71: error: declaration of C function ‘std:stream& operator<<(std:stream&, mpq_srcptr)’ conflicts with
__GMP_DECLSPEC_XX std:stream& operator<< (std:stream &, mpq_srcptr);
                                                                       ^
/usr/local/include/gmp.h:2250:33: error: previous declaration ‘std:stream& operator<<(std:stream&, mpz_srcptr)’ here
__GMP_DECLSPEC_XX std:stream& operator<< (std:stream &, mpz_srcptr);
                                 ^
/usr/local/include/gmp.h:2252:71: error: declaration of C function ‘std:stream& operator<<(std:stream&, mpf_srcptr)’ conflicts with
__GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpf_srcptr);
                                                                       ^
/usr/local/include/gmp.h:2251:33: error: previous declaration ‘std::ostream& operator<<(std::ostream&, mpq_srcptr)’ here
__GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpq_srcptr);
                                 ^
/usr/local/include/gmp.h:2252:71: error: declaration of C function ‘std::ostream& operator<<(std::ostream&, mpf_srcptr)’ conflicts with
__GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpf_srcptr);
                                                                       ^
/usr/local/include/gmp.h:2250:33: error: previous declaration ‘std::ostream& operator<<(std::ostream&, mpz_srcptr)’ here
__GMP_DECLSPEC_XX std::ostream& operator<< (std::ostream &, mpz_srcptr);
                                 ^
/usr/local/include/gmp.h:2254:68: error: declaration of C function ‘std::istream& operator>>(std::istream&, mpq_ptr)’ conflicts with
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpq_ptr);
                                                                    ^
/usr/local/include/gmp.h:2253:33: error: previous declaration ‘std::istream& operator>>(std::istream&, mpz_ptr)’ here
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpz_ptr);
                                 ^
/usr/local/include/gmp.h:2255:68: error: declaration of C function ‘std::istream& operator>>(std::istream&, mpf_ptr)’ conflicts with
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpf_ptr);
                                                                    ^
/usr/local/include/gmp.h:2254:33: error: previous declaration ‘std::istream& operator>>(std::istream&, mpq_ptr)’ here
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpq_ptr);
                                 ^
/usr/local/include/gmp.h:2255:68: error: declaration of C function ‘std::istream& operator>>(std::istream&, mpf_ptr)’ conflicts with
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpf_ptr);
                                                                    ^
/usr/local/include/gmp.h:2253:33: error: previous declaration ‘std::istream& operator>>(std::istream&, mpz_ptr)’ here
__GMP_DECLSPEC_XX std::istream& operator>> (std::istream &, mpz_ptr);
                                 ^
make: *** [main.o] 错误 1

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2014-12-24 21:22 |只看该作者
test.h 改成

#include <gmp.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void print_msg(mpz_t message);

#ifdef __cplusplus
}
#endif

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:032015年亚洲杯之中国
日期:2015-04-22 15:52:45
3 [报告]
发表于 2014-12-25 10:37 |只看该作者
同意楼上, 虽然不知道gmp是嘛玩意, 但看错误感觉它是c++写的...

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
4 [报告]
发表于 2014-12-25 10:58 |只看该作者
编译gcc源代码的时候就会用到这个库回复 3# hanxin83


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP