1

UbuntuでEclipseを使用しています。

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

1 に答える 1

2

わかりました、ここで答えを見つけました。そこから引用します。

-L オプションは、シェルの $PATH が実行可能ファイルの検索パスであるように、ライブラリの検索パスのように機能します。

また、シェルにデフォルトの検索パスがあるように、リンカーにもデフォルトのライブラリ検索パスがあり、/usr/lib を含める必要があります。したがって、-L/usr/lib オプションを使用する必要さえないはずです。それがうまくいかなかった理由は、 -l オプションでフルパスを使用しているためです。

通常、 -l オプションを使用すると、「拡張子」はファイル名、 lib 接頭辞、および directory から除外されます

したがって、私の場合、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

これは正常にリンクします。

もちろん、私はこれを知っていましたが、忘れていました。ああ!

于 2015-10-22T06:54:52.533 に答える