c - use extern structure variable defined in a library, config with 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; } 

here my_test_my library file of test.h , test.c compiled 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 = 0;   int c = 1;   c = + c; } 

write cmakelists.txt , use cmake generated 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) 

after generate makefiles in out directory, execute 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 library: libmy_test_my.a table of contents empty (no object file members in 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 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 architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation) make[2]: *** [tutorial] error 1 make[1]: *** [cmakefiles/tutorial.dir/all] error 2 make: *** [all] error 2 

why happened ?


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -