2015-10-21 78 views
1

我在Ubuntu下使用Eclipse。如何链接cmocka?

我刚刚安装cmocka:

Install the project... 
-- Install configuration: "Debug" 
-- Installing: /usr/lib/pkgconfig/cmocka.pc 
-- Installing: /usr/lib/cmake/cmocka/cmocka-config.cmake 
-- Installing: /usr/lib/cmake/cmocka/cmocka-config-version.cmake 
-- Installing: /usr/include/cmocka.h 
-- Installing: /usr/include/cmocka_pbc.h 
-- Installing: /usr/lib/libcmocka.so.0.3.1 
-- Installing: /usr/lib/libcmocka.so.0 
-- Installing: /usr/lib/libcmocka.so 

当我建立一个简单的测试项目中,我得到的链接错误。从这个代码

#include <stdarg.h> 
#include <stddef.h> 
#include <setjmp.h> 
#include <cmocka.h> 

#include "factorial.h" 

static void test_factorial_zeo() 
{ 
    assert_int_equal(factorial(0), 1); 
} 

int main(int argc, char **argv) 
{ 
    const UnitTest tests[] = 
    { 
      unit_test(test_factorial_zeo), 
    }; 

    return run_tests(tests); 
} 

我得到这些错误:

make all 
Building target: unit_test_C_code_example_project 
Invoking: GCC C Linker 
gcc -o "unit_test_C_code_example_project" ./test_scripts/test_factorial.o ./software_under_test/factorial.o 
./test_scripts/test_factorial.o: In function `test_factorial_zeo': 
/home/me/workspace/unit_test_C_code_example_project/Debug/../test_scripts/test_factorial.c:10: undefined reference to `_assert_int_equal' 
./test_scripts/test_factorial.o: In function `main': 
/home/me/workspace/unit_test_C_code_example_project/Debug/../test_scripts/test_factorial.c:20: undefined reference to `_run_tests' 
collect2: ld returned 1 exit status 
make: *** [unit_test_C_code_example_project] Error 1 

**** Build Finished **** 

所以,看来我应该向cmocka库添加到链接器的路径。但后来我得到

make all 
Building target: unit_test_C_code_example_project 
Invoking: GCC C Linker 
gcc -o "unit_test_C_code_example_project" ./test_scripts/test_factorial.o ./software_under_test/factorial.o -llibcmocka.so.0.3.1 
/usr/bin/ld: cannot find -llibcmocka.so.0.3.1 
collect2: ld returned 1 exit status 
make: *** [unit_test_C_code_example_project] Error 1 

**** Build Finished **** 

我得到相同的结果与libcmocka.so.0.3.1,libcmocka.so.0和libcmocka.so

很显然,我做一些很基本的错误,但什么?

ls -lAF /usr/lib/libcmocka.so* 
lrwxrwxrwx 1 root root 14 Oct 21 15:03 /usr/lib/libcmocka.so -> libcmocka.so.0* 
lrwxrwxrwx 1 root root 18 Oct 21 15:03 /usr/lib/libcmocka.so.0 -> libcmocka.so.0.3.1* 
-rwxrwxrwx 1 root root 77216 Oct 21 15:02 /usr/lib/libcmocka.so.0.3.1* 

回答

2

好吧,我找到了答案here将从中引用:

-L选项就像图书馆搜索路径,就像在shell $ PATH 是一个搜索路径用于可执行文件。

就像shell有默认的搜索路径一样,链接器也有 默认的库搜索路径,应该包含/ usr/lib。所以你 甚至不需要使用-L/usr/lib选项。原因 为什么没有为你工作是你使用-l 选项的完整路径。

通常使用-l选项时,文件 名称以及lib前缀和目录中省略了“扩展名”。

所以,在我的情况,我告诉Eclipse中cmocka链接,导致与此命令

gcc -L/usr/lib -o "unit_test_C_code_example_project" ./test_scripts/test_factorial.o ./software_under_test/factorial.o -lcmocka 

成功地链接生成makefile文件。

当然,我知道这个,但已经忘记了它。 D'哦!