如何链接到共享库在c + +
问题描述:
有人可以帮助我链接到共享库,特别是libzmq,在C + +?如何链接到共享库在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
我已经安装使用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
,并解决了这个问题。它并没有告诉我有关如何识别磁盘上的共享库和头文件并链接到它的任何信息......但我想我可以将它移到另一天。
答
ZeroMQ(zmq.hpp)的C++包装器不再是ZeroMQ的一部分。目前的libzmq master或最新的stable 3.2.x中没有zmq.hpp。
这不是一个链接错误。 '这是一个编译器错误,尤其是无法找到您的头文件。仔细检查你的包含路径。 – WhozCraig 2013-02-13 17:51:36
你确定'/ usr/local/include'中有'zmq /'子目录吗?在Debian的'libzmq-dev'上,没有这样的子目录,你直接''#include' –
2013-02-13 17:52:47
@AntonKovalenko - 我从源码安装libzmq,如上面“我如何安装libzmq”部分所述。 – bitcycle 2013-02-13 17:54:50