简体   繁体   English

使用 ta-lib/ta_lib.h 文件和 Makefile 时未定义引用

[英]Undefined reference when using ta-lib/ta_lib.h file and Makefile

I want to use the ta_lib functions in my C code and am trying to import the ta_lib functions.我想在我的 C 代码中使用 ta_lib 函数,并尝试导入 ta_lib 函数。 The header file gets imported correctly but i cannot get the linker to find the actual library. header 文件已正确导入,但我无法通过 linker 找到实际的库。 I want to do the compiling process with MAKE and gcc.我想用 MAKE 和 gcc 进行编译过程。

Firstly I import the header首先我导入 header

#include <ta-lib/ta_libc.h>

And then when i need to use a function然后当我需要使用 function


TA_ADOSC(0, CSV_LENGTH - 1, temp_high, temp_low, temp_close, temp_volume, 3, 10, &beginIdx, &endIdx, tmp_adosc);

The program compiles fine using my makefile该程序使用我的 makefile 编译得很好


# create CC variable

CC = gcc

# create CFLAGS variable

CFLAGS =  -L/usr/local/lib -Wall -g

LDLIBS = -lta_lib -I/usr/local/include -lm

output: main.o
$(CC) $(CFLAGS) -o output main.o

main.o: main.c
$(CC) $(LDLIBS) -c main.c

# target: dependencies

# action

clean:
rm -f \*.o output

Once I try to run make i get the following一旦我尝试运行make我得到以下信息


gcc -L/usr/local/lib -Wall -g -o output main.o
/usr/bin/ld: main.o: in function `calculate_indicators': main.c:(.text+0x226): undefined reference to `TA_ADOSC'
collect2: error: ld returned 1 exit status
make: \*\*\* \[Makefile:10: output\] Error 1

From my understanding I need to fix the linking to the shared library.根据我的理解,我需要修复到共享库的链接。

The library is installed:库已安装:


ldconfig -p | grep libta_lib.so

Returns the following返回以下内容


    libta_lib.so.0 (libc6,x86-64) => /usr/local/lib/libta_lib.so.0
    libta_lib.so.0 (libc6,x86-64) => /lib/libta_lib.so.0
    libta_lib.so (libc6,x86-64) => /usr/local/lib/libta_lib.so
    libta_lib.so (libc6,x86-64) => /lib/libta_lib.so

Since i am fairly new to C and using external libraries I can't find what seems to be the problem由于我是 C 的新手并且使用外部库我找不到问题所在

You are adding the libraries to the compile line.您正在将库添加到编译行。 They need to be added to the link line.它们需要添加到链接行。 And preprocessor options like -I are used by the compiler , and "where to find libraries" options like -L are used by the linker .编译器使用像-I这样的预处理器选项, linker使用像-L这样的“在哪里可以找到库”选项。

Also, libraries always must come at the end of the link line, after all the object files.此外,库始终必须位于链接行的末尾,在所有 object 文件之后。 And, the -L "where to search" option should come before the -l "what library to find" option.并且, -L “在哪里搜索”选项应该出现在-l “要查找的库”选项之前。

Write your rules like this:像这样写你的规则:

CFLAGS = -I/usr/local/include -Wall -g
LDFLAGS = -L/usr/local/lib
LDLIBS = -lta_lib -lm

output: main.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o output main.o $(LDLIBS)

main.o: main.c
        $(CC) $(CFLAGS) -c main.c

However, it's better to just let make do the work for you;但是,最好让 make 为您完成工作; it knows how to correctly compile things (as long as you set the standard variables).它知道如何正确编译东西(只要你设置了标准变量)。 You don't need to include a rule to build main.o at all.您根本不需要包含构建main.o的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM