bitcycle
bitcycle

Reputation: 7792

How to link to shared lib in c++

Could someone help me with linking to a shared lib, specifically libzmq, in C++?

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

I've installed libzmq using the following steps:

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

Here's my 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;                                                             
}                                                                         

And, here's the error msg:

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

The error is pretty straight forward, but I've yet to find a solution. Any ideas?

My goal is to do something like this with multiple child processes.

Update I'm just going to install it system-wide in ubuntu: sudo apt-get install libzmq-dev, and that resolved the issue. It doesn't teach me anything about how to identify a shared lib and header file on disk and link to it... but I guess I can move that to another day.

Upvotes: 0

Views: 2046

Answers (1)

vond
vond

Reputation: 1948

C++ wrapper for ZeroMQ (zmq.hpp) is no longer part of ZeroMQ. There is no zmq.hpp in current libzmq master or in latest stable 3.2.x.

Upvotes: 1

Related Questions