0

我正在尝试使用 libftp 构建一个基本的 FTP 客户端。我已将其编译并归档libftp.a/usr/local/lib. 我已经放入所有必要的标题/usr/local/include/ftp

在 Build Settings 下,我将“Header Search Paths”设置为/usr/local/include,并将“Library Search Paths”设置为/usr/local/lib。对于“其他链接器标志”,我添加了-lftp.

这是我的 C++ 类的外壳:

连接器.h:

#include <stdlib.h>
#include <ftp/ftp.h>
#include <stdio.h>

class Connector{
    private:
        FtpConnection *connection;
    public:
        Connector();
        ~Connector();

        bool connect(const char *hostname, const char *port);
    };

连接器.cc:

#include "Connector.h"

Connector::Connector(){
    }

Connector::~Connector(){
    }

bool Connector::connect(const char *hostname, const char *port){
    ftpGetAddresses(hostname, port);
    printf("Connected!\n");
    return true;
    }

编译后,这是我得到的错误:

架构 x86_64 的未定义符号:“ftpGetAddresses(char const*, char const*)”,引用自:Connector.o ld 中的 Connector::connect(char const*, char const*):未找到架构 x86_64 的符号clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

可能值得注意的是,这是 Cocoa 项目的一部分,因此Connector该类#included位于 myAppDelegate中,这当然是一个 Obj-C 类。我所有的 Obj-C 源文件都有.mm扩展名。

我确信该库处于正常工作状态,因为我在命令行上使用gcc ... -lftp. 这只是 Xcode 的一个问题。

4

1 回答 1

1

Well, it appears I just talked myself through my own problem. As I was typing the last part of my question, I realized that the issue was linking a C library in a C++ source file. gcc would compile just fine on command line, but g++ gave me the same error as Xcode. One google search later I found this link, which solved my problem beautifully. Basically, if you want a C library to be compatible with C++, you need to add

#ifdef __cplusplus
extern "C" {
#endif

at the top of the library header file, and add

#ifdef __cplusplus
}
#endif

at the bottom of the file. I'll leave the question here hoping it will help someone else in the future.

于 2013-08-19T21:25:54.677 回答