在osx中​​构建共享库(.so)

时间:2013-10-10 10:32:25

标签: c macos gcc shared-libraries

我正在尝试构建以下代码:

#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机器上成功地将这些代码编译成一个共享库。也许这个问题存在于我的配置中?但是我看不到哪里

1 个答案:

答案 0 :(得分:1)

您已声明了这些功能:

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()看起来是未来重复符号链接器错误的根本原因......

OS X也使用.dylib文件扩展名,而不是.so来表示动态对象。