使用库中定义的extern结构变量,使用cmake进行配置

时间:2016-08-24 00:16:01

标签: c cmake

的main.c

#include "test.h"
extern struct test_struct my_test_str; // defined in my_test_lib.a 
int main(int argc, char *argv[]) {
   return 0;
}

这是test.h和test.c的my_test_my库文件 编译为my_test_lib.a

test.h

#ifndef TEST_MY
#define TEST_MY
struct test_struct {
  double d1;
  double d2;
  double d3;
};
void ddd(void);
#endif

test.c的

struct test_struct my_test_str;
void ddd(void) {
  int a = 0;
  int c = 1;
  c = a + c;
}

编写CMakeLists.txt,使用cmake生成Makefile

cmake_minimum_required(VERSION 2.6)
project(Tutorial)

add_library(my_test_my test.c)
add_executable(Tutorial main.c)
target_link_libraries (Tutorial my_test_my)

在out目录中生成Makefile后,执行make

192:out jerryw$ make
[ 25%] Building C object CMakeFiles/my_test_my.dir/test.c.o
[ 50%] Linking C static library libmy_test_my.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: libmy_test_my.a(test.c.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: libmy_test_my.a(test.c.o) has no symbols
warning: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: warning for library: libmy_test_my.a the table of contents is empty (no object file members in the library define global symbols)
[ 50%] Built target my_test_my
[ 75%] Building C object CMakeFiles/Tutorial.dir/main.c.o
[100%] Linking C executable Tutorial
Undefined symbols for architecture x86_64:
  "_ddd", referenced from:
      _main in main.c.o
  "_my_test_str", referenced from:
      _main in main.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Tutorial] Error 1
make[1]: *** [CMakeFiles/Tutorial.dir/all] Error 2
make: *** [all] Error 2

为什么会这样?

0 个答案:

没有答案