V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xwbz2018
V2EX  ›  C

新手求助, error: template with C linkage

  •  
  •   xwbz2018 · 2019-08-18 10:15:32 +08:00 · 4827 次点击
    这是一个创建于 1684 天前的主题,其中的信息可能已经有所发展或是发生改变。

    g++版本7.4.0

    编译命令
    g++ -c -std=c++11 -O3 -o myport myport.cpp -lrt -lpthread -Icore -Lcore

    报错部分:

    In file included from /usr/include/ctype.h:39:0,
                     from /usr/include/c++/7/cctype:42,
                     from /usr/include/c++/7/bits/localefwd.h:42,
                     from /usr/include/c++/7/string:43,
                     from core/***.h:26,
                     from core/***.h:4,
                     from myport.cpp:4:
    core/***.h:29:1: error: template with C linkage
     template<bool ToLittle>
     ^~~~~~~~
    In file included from core/***.h:30:0,
                     from core/***.h:4,
                     from myport.cpp:4:
    /usr/include/netinet/tcp.h:102:17: error: redeclaration of ‘ uint8_t tcphdr::<unnamed union>::<unnamed struct>::th_off ’
      uint8_t th_off:4; /* data offset */
                     ^
    /usr/include/netinet/tcp.h:99:17: note: previous declaration ‘ uint8_t tcphdr::<unnamed union>::<unnamed struct>::th_of ’
      uint8_t th_off:4; /* data offset */
                     ^
    /usr/include/netinet/tcp.h:103:16: error: redeclaration of ‘ uint8_t tcphdr::<unnamed union>::<unnamed struct>::th_x2 ’
      uint8_t th_x2:4; /* (unused) */
                    ^
    /usr/include/netinet/tcp.h:98:16: note: previous declaration ‘ uint8_t tcphdr::<unnamed union>::<unnamed struct>::th_x2 ’
      uint8_t th_x2:4; /* (unused) */
    

    由于强行转的 c/c++,很多基础不知道,来求助一下大佬们。迫于保密,部分已打码。

    看描述有两个问题:

    1. error: template with C linkage

    搜了下,这个很多是 extern "C"导致的,我这边的是:

    #ifdef __cplusplus
    extern "C" {
    #endif
    #include "myport.h"
    #ifdef __cplusplus
    }
    #endif
    

    整段去掉都没用

    2. 报错地点引用了以下头文件

    #include <string>
    #include <strings.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>       <-- 这行是 core/***.h:30:0
    #include <arpa/inet.h>
    
    第 1 条附言  ·  2019-08-18 23:35:17 +08:00

    我把myport.cpp和myport.h移到core文件夹里了,然后就不报这两个错误了,并不是头文件冲突的问题@ipwx @pagxir ,因为另外有其他能运行的文件(加个main方法就能编译运行那种)。使用以下命令编译通过:

    g++ -c -std=c++11 -O3 -o myport myport.cpp -lrt -lpthread

    不过出现了更多奇怪的问题=_=!

    某个class里有个bool变量,作为配置使用,默认值是false,使用时未修改该值。 然鹅在使用 printf("%d\n", flag) 后,显示为[ 0、48、64、128、192 ] 随机一个。 然后之后就不知道运行到哪里 Segmentation fault (core dumped),挂掉了。

    然后我猜测可能是优化问题,就删掉了-O3,结果编译不通过,提示:

    undefined reference to `MyConf::BufMaxSize'

    这是一个派生类,就放在myport.cpp里,结构如下:

    #include "common.h"
    struct MyConf : public CommonConf{
        static const int BufMaxSize = 2048;
    }
    

    基类CommonConf在common.h

    struct CommonConf
    {
       //  其他配置,没有BufMaxSize
    }
    

    明天我再查一下是哪里挂了

    第 2 条附言  ·  2019-11-21 14:00:30 +08:00
    的确是跟 /usr/include/ctype.h 里引用的系统头文件重名了。。。
    9 条回复    2019-11-21 14:00:13 +08:00
    ipwx
        1
    ipwx  
       2019-08-18 10:47:07 +08:00
    似乎是你引用的头文件和你的明明冲突了。

    建议把 TCP 相关代码封装到一个类里面,然后通过 .h & .cpp 隔离 netinet/tcp.h,不污染你程序别的地方。
    xwbz2018
        2
    xwbz2018  
    OP
       2019-08-18 11:13:30 +08:00
    @ipwx 谢谢,我试下看看。不过除了自己写的 myport.cpp ,其他都是.h 文件
    Nasei
        3
    Nasei  
       2019-08-18 11:20:47 +08:00 via Android
    string
    xwbz2018
        4
    xwbz2018  
    OP
       2019-08-18 11:31:26 +08:00 via Android
    @Nasei ???是哪里写错了吗?
    pagxir
        5
    pagxir  
       2019-08-18 11:31:47 +08:00 via Android
    #ifdef __cplusplus
    extern "C" {
    #endif
    #include "myport.h"
    #ifdef __cplusplus
    }
    #endif
    把这里的 extern c 去掉,因为 extern c 表明代码是 c 的代码,但是你的 myport.h 却使用了模板,这明显是 cpp 的代码。这不是自相矛盾么
    xwbz2018
        6
    xwbz2018  
    OP
       2019-08-18 11:37:29 +08:00 via Android
    @pagxir #5 myport. h 里面就定义了结构体和方法,没有 template,连导入头文件都没有,template 是 core 文件夹里的一个头文件定义的,而且我把 extern 删掉都一样的结果
    pagxir
        7
    pagxir  
       2019-08-18 11:42:31 +08:00 via Android   ❤️ 1
    那就是你文件名命名有问题,跟系统头文件冲突了
    xwbz2018
        8
    xwbz2018  
    OP
       2019-08-18 11:51:57 +08:00 via Android
    @pagxir 谢谢,我看看是不是
    xwbz2018
        9
    xwbz2018  
    OP
       2019-11-21 14:00:13 +08:00
    @pagxir #7 的确是跟 /usr/include/ctype.h 里引用的系统头文件重名了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3213 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:38 · PVG 19:38 · LAX 04:38 · JFK 07:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.