簡體   English   中英

如何在C ++中鏈接到共享庫

[英]How to link to shared lib in c++

有人可以幫助我鏈接到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

該錯誤非常簡單,但是我還沒有找到解決方案。 有任何想法嗎?

我的目標是做一些像這樣具有多個子進程。

更新我將在ubuntu中將其安裝在整個系統范圍內: sudo apt-get install libzmq-dev ,這解決了該問題。 它沒有教我任何有關如何識別磁盤上共享的lib和頭文件並鏈接到它的信息……但是我想我可以將它轉移到另一天。

ZeroMQ(zmq.hpp)的C ++包裝器不再是ZeroMQ的一部分。 當前的libzmq主目錄或最新的穩定版3.2.x中沒有zmq.hpp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM