如何与cmocka连接?

时间:2023-01-30 16:27:16

I am using Eclipse under Ubuntu.

我在Ubuntu下使用Eclipse。

I just installed 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

When I build a simple test project, I get linker errors. From this code

当我构建一个简单的测试项目时,我将得到链接器错误。从这段代码

#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);
}

I get these errors:

我得到这些错误:

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 ****

So, it seems that I ought to add the cmocka library to the linker path. But then I get

因此,我似乎应该将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 ****

I get the same results with libcmocka.so.0.3.1, libcmocka.so.0 and libcmocka.so

libcmocka。0。1 libcmocka。0和libcmocka.so

Obviously, I am doing something very basic wrongly, but what?

显然,我做了一些非常基本的错误,但是什么呢?

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*

1 个解决方案

#1


2  

Ok, I found the answer here and will quote from it:

好的,我在这里找到了答案,我会引用它:

The -L option works like a search path for libraries, just like $PATH in the shell is a search path for executable files.

l选项的工作方式类似于库的搜索路径,就像shell中的$ path是可执行文件的搜索路径一样。

And just like the shell has a default search path, the linker also has a default library search path, with should include /usr/lib. So you should not even need to have to use a -L/usr/lib option. The reason why that did not work for you is that you use a full path with the -l option.

就像shell有一个默认的搜索路径一样,链接器也有一个默认的库搜索路径,其中应该包含/usr/lib所以你甚至不需要使用-L/usr/lib选项。这对您不起作用的原因是您使用带有-l选项的完整路径。

Normally with the -l option the "extension" is left out from the file name, as well the lib prefix, and the directory.

通常使用-l选项时,“扩展名”会从文件名、lib前缀和目录中删除。

So, in my case, I told Eclipse to link with cmocka, which caused the makefile to be generated with this command

因此,在我的例子中,我告诉Eclipse与cmocka链接,这导致使用这个命令生成makefile

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

which links successfully.

连接成功。

Of course, I knew this , but had forgotten it. D'oh!

当然,我知道这一点,但已经忘记了。分析!

#1


2  

Ok, I found the answer here and will quote from it:

好的,我在这里找到了答案,我会引用它:

The -L option works like a search path for libraries, just like $PATH in the shell is a search path for executable files.

l选项的工作方式类似于库的搜索路径,就像shell中的$ path是可执行文件的搜索路径一样。

And just like the shell has a default search path, the linker also has a default library search path, with should include /usr/lib. So you should not even need to have to use a -L/usr/lib option. The reason why that did not work for you is that you use a full path with the -l option.

就像shell有一个默认的搜索路径一样,链接器也有一个默认的库搜索路径,其中应该包含/usr/lib所以你甚至不需要使用-L/usr/lib选项。这对您不起作用的原因是您使用带有-l选项的完整路径。

Normally with the -l option the "extension" is left out from the file name, as well the lib prefix, and the directory.

通常使用-l选项时,“扩展名”会从文件名、lib前缀和目录中删除。

So, in my case, I told Eclipse to link with cmocka, which caused the makefile to be generated with this command

因此,在我的例子中,我告诉Eclipse与cmocka链接,这导致使用这个命令生成makefile

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

which links successfully.

连接成功。

Of course, I knew this , but had forgotten it. D'oh!

当然,我知道这一点,但已经忘记了。分析!