在OS X Mavericks中将C与C ++链接起来 [英] Linking C from C++ in OS X Mavericks
问题描述
从我的makefile生成的一对命令是:
gcc`pkg-config --cflags glib-2.0 `-g -Wall -O3`pkg-config --cflags flann` -c -o vec.o vec.c
g ++`pkg-config --cflags glib-2.0` -g -Wall -O3 -stdlib = libstdc ++ -lstdc ++ layoutquality.cpp vec.o`pkg-config --libs glib-2.0` -l / usr / local / Cellar / flann / 1.8.4 / lib -lflann -o layoutquality
连结器抱怨:
未定义体系结构x86_64的符号:
load_dmat(char const *),引用来自:
_main in layoutquality -I8HOqy.o
ld:符号(s)未找到架构x86_64
其中 load_dmat
只是文件vec.c中的一个函数。如果我在第一行用 g ++
代替 gcc
,那么所有内容都会编译并链接正常,但clang说:
clang:warning:在C ++模式下将'c'输入视为'c ++',此行为已被弃用
是否有一种无害的,不被废弃的编译和链接方式?在升级到OS X Mavericks和新的命令行工具之前,与 g ++
以及来自 gcc
的对象文件进行链接。任何洞察什么改变以及如何前进将是非常好的,谢谢。
如果您在同一行中编译多个.c / .cpp文件,您可以使用-xc或 - x C ++在C或C ++文件名的每个列表之前切换上下文。例如:
g ++ -xc alpha.c beta.c -x c ++ gamma.cpp
Having transitioned to OS X Mavericks and XCode 5.0.1, I can no longer gracefully link compiled C files (output from gcc) to a C++ project (output from g++).
The offending pair of commands produced from my makefile are:
gcc `pkg-config --cflags glib-2.0` -g -Wall -O3 `pkg-config --cflags flann` -c -o vec.o vec.c
g++ `pkg-config --cflags glib-2.0` -g -Wall -O3 -stdlib=libstdc++ -lstdc++ layoutquality.cpp vec.o `pkg-config --libs glib-2.0` -L/usr/local/Cellar/flann/1.8.4/lib -lflann -o layoutquality
To which the linker complains:
Undefined symbols for architecture x86_64: "load_dmat(char const*)", referenced from: _main in layoutquality-I8HOqy.o ld: symbol(s) not found for architecture x86_64
Where load_dmat
is just a function in the file vec.c . If I replace the gcc
with g++
in the first line, then everything compiles and links fine, but clang says:
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Is there an inoffensive, non-deprecated way of compiling and linking these? Linking with g++
together with object files from gcc
worked fine before I upgraded to OS X Mavericks and the new command line tools. Any insight into what changed and how to go forward would be great, thanks.
Adding a "-x c" (without quotes) before "vec.c" should fix it.
If you compile multiple .c/.cpp files in the same line you can use "-x c" or "-x c++" before each list of C or C++ filenames to switch context appropriately. For example:
g++ -x c alpha.c beta.c -x c++ gamma.cpp
这篇关于在OS X Mavericks中将C与C ++链接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!