我正在尝试构建以下代码:
#include <stdio.h>
#include "defs.h"
struct polynome saisie(void);
struct polynome mult (struct polynome, struct polynome);
/* ************************************************
produit
Produit de 2 polynomes saisis au sein de la fonction
entree : -
sortie : -
**************************************************** */
void produit(void) {
struct polynome P1,P2,Q;
int i;
printf("Premier polynome : \n");
P1=saisie();
printf("Second polynome : \n");
P2=saisie();
Q=mult(P1,P2);
for(i=Q.degre; i>=0; i--)
printf("coefficient de X a la puissance %d : %d\n",i, Q.coef[i]);
printf("\n");
}
使用这个命令:
gcc -shared -o lib/libop.so lib/*.o
而且我总是得到这个错误:
Undefined symbols for architecture x86_64:
"_saisie", referenced from:
_produit in produit.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不知道它是否对您有帮助,但这是我的 gcc -v 输出:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
编辑:这是包含的标题
#define N 10
struct polynome {
int degre;
int coef[N];
};
此外,我会说我的一些同事在 linux 机器上成功地将这段代码编译成共享库。也许问题出在我的配置上?但是我看不到哪里
最佳答案
你已经声明了这些函数:
struct polynome saisie(void);
struct polynome mult (struct polynome, struct polynome);
但你还没有实现它们。
同时复制 struct
,而不是将指针传递给它们,看起来效率有点低,因为它们的大小并不小,所以我会用这些语义实现这些方法:
void saisie(struct polynome *out);
void mult(const struct polynome *in1, const struct polynome *in2, struct polynome *out);
如果有意义,可能会返回一些状态。此外,名称 mult()
看起来像是 future 重复符号链接(symbolic link)器错误的根本原因......
此外,OS X 还对动态对象使用 .dylib
文件扩展名,而不是 .so
。
关于c - 在 osx (.so) 中构建共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19293362/