C code compiled with C++: undefined reference

前端 未结 4 1644
一个人的身影
一个人的身影 2021-02-14 23:31

I have a small program that I can compile with GCC and ICC without any difficulties, but I would also like the code to work with G++ and ICPC. I tried to add this:



        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-15 00:23

    I think you're getting it wrong... The extern C is for disabling the function mangling; so if you do it just for the header files, when you try to link your mangled object code, the declared function names won't match with the function names in the object file.

    Anyway, the extern C won't add any portability if the whole application is being compiled and linked with the same C++ compiler, it's intended for mixing C libraries with C++ code.

    If your code is in the common subset of C and C++, you should be already able to compile it with either compiler, but I cannot see the reason to do that (besides working on the principle of least surprise, as C++ is more strict with some things).

提交回复
热议问题