【问题标题】:How to link to shared lib in c++如何在 C++ 中链接到共享库
【发布时间】:2013-02-13 17:49:02
【问题描述】:

有人可以帮我链接到 C++ 中的共享库,特别是 libzmq 吗?

all: clean compile                                        

clean:                                                    
    rm bin *.o -f                                         

compile:                                                  
    g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin

我使用以下步骤安装了 libzmq:

git clone https://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure
make && sudo make install

这是我的 main.cpp

#include <iostream>                                                       
#include <string>                                                         

#include <zmq/zmq.h>                                                    

// Required by fork routine                                               
#include <sys/types.h>                                                    
#include <unistd.h>                                                       

// Required by wait routine                                               
#include <sys/wait.h>                                                     

#include <stdlib.h>         // Declaration for exit()                     
#include <cstdio>           // printf                                     
using namespace std;                                                      

int global_variable = 2;                                                  

int main(int argc, char** argv){                                          
    const short int FORK_FAILED = -1;                                     
    const short int FORK_SUCCESS = 0;                                     
    int stack_variable = 20;                                              
    pid_t pid;                                                            
    string status_identifier;                                             
    switch (pid = fork()){                                                
        case FORK_SUCCESS:                                                
            printf("Child changing global and stack variables\n");        
            global_variable++;                                            
            stack_variable++;                                             
            break;                                                        
        case FORK_FAILED:                                                 
            cerr << "Failed!  -- Failed to fork:  " << pid << endl;       
            exit(1);                                                      
        default:                                                          
            printf("Child process (pid=%d) created successfully.\n", pid);
            wait(0);                                                      
            break;                                                        
    }                                                                     
    printf("[pid=%d] Global:  %d\n", pid, global_variable);               
    printf("[pid=%d] Stack:  %d\n", pid, stack_variable);                 
    return 0;                                                             
}                                                                         

而且,这是错误消息:

bitcycle @ ubuntu64vm ~/git/test $ make
rm bin *.o -f
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
main.cpp:4:23: fatal error: zmq/zmq.hpp: No such file or directory
compilation terminated.
make: *** [compile] Error 1

错误很简单,但我还没有找到解决方案。有什么想法吗?

我的目标是使用多个子进程执行this 之类的操作。

更新我将在 ubuntu 中将其安装到系统范围内:sudo apt-get install libzmq-dev,这样就解决了问题。它没有教我如何识别磁盘上的共享库和头文件并链接到它......但我想我可以将它移到另一天。

【问题讨论】:

  • 这不是链接错误。 '这是一个编译器错误,特别是找不到你的头文件。仔细检查您的包含路径。
  • 您确定/usr/local/include 中有zmq/ 子目录吗?在debian上libzmq-dev,没有这个子目录,你直接#include &lt;zmq.hpp&gt;
  • @AntonKovalenko -- 我从源代码安装了 libzmq,如上面“我如何安装 libzmq”部分所述。
  • 我读过你写的。关键是,发行版通常在打包内容时移动头文件,除非他们有充分的理由这样做。这就是为什么在没有中间zmq/ 目录的情况下,在/usr/local/include 下找到zmq.hpp 的机会很大。
  • @bitcycle 阅读 Bo 的评论。您的源文件中需要#include &lt;zmq.h&gt;。 *没有zmq/zmq.h,因为在包含路径中的任何文件夹中都没有zmq文件夹,其中包含一个名为zmq.h的文件

标签: c++ makefile shared-libraries zeromq


【解决方案1】:

ZeroMQ 的 C++ 包装器 (zmq.hpp) 不再是 ZeroMQ 的一部分。当前的 libzmq master 或最新的稳定版 3.2.x 中没有 zmq.hpp。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多