无法编译在cpp中使用另一个模块的程序

问题描述 投票:0回答:1

我正在编写一个 cpp 程序,我想在其中包含另一个自定义库,这样我就不需要添加多个文件等进行编译。 我的项目结构是:

apps/
    - /test
    - /test2

我已将测试库包含在 test2 的 makefile 中:

STATIC_LIBS     += test
CFLAGS          += -I$(SWIFT_ROOT)/apps/test/
LDFLAGS         += -L$(SWIFT_ROOT)/apps/test

但仍在编译时我收到此错误:

/usr/bin/ld: cannot find -ltest
collect2: error: ld returned 1 exit status

生成文件:

ENABLE_VERSION_API=1

TARGET = test2

SRCS += \
        main.cpp \
        test2.cpp \
        \
        # Add more

LIBS            +=
STATIC_LIBS     += test
EXT_STATIC_LIBS += ssl crypto ssh2 curl protobuf
EXT_LIBS        += z

CFLAGS    += -I$(SWIFT_ROOT)/include/google/protobuf/
CFLAGS    += -I$(SWIFT_ROOT)/libs/src/test2/
CFLAGS    += -I$(SWIFT_ROOT)/apps/test/
LDFLAGS   += -L$(SWIFT_ROOT)/apps/test
include $(SWIFT_ROOT)/build/app.mak

执行命令-

    g++ -I/root/include/google/protobuf/ - 
   I/root/libs/src/test2/  -std =c++17 -Wno-unused-but- 
   set-variable -Wno-misleading-indentation -Wno-deprecated- 
   declarations -Wno-parentheses -Wno-switch -Wno-literal    -suffix - 
   Wno-conversion-null -Wno-format-contains-nul  -D_THREAD_SAFE - 
   D_GNU_SOURCE -D__USE_GNU .builtobjs/main.o .builtobjs/test2.o -L 
   /root/apps/test -Xlinker --start-group -lz -ldl 
   -lpthread     -lrt -luuid -lstdc++fs -Wl,-Bstatic -lssl -lcrypto - 
   lssh2 -lcurl -lprotobuf -ltest -Wl,-Bdynamic - 
   Xlinker --end-grou    p -Wl,-rpath,/opt/swift/lib64 -Wl,- 
   rpath,/opt/swift/lib -o test2

这里我的主要问题是如何在另一个模块中添加自定义库。我遵循了标准步骤,例如包括 LIB、CFLAG (-I) 和 LDFLAG (-L)。但是这里还有其他需要改进的地方吗?

c++ c gcc makefile lib
1个回答
0
投票

正如一些人指出的那样。我的目录中没有 .a 文件。使用 ar 命令生成它,现在工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.