1

I am trying to create a shared library and trying to load it.

I have created a header file cqueue.h

 /* * File: cqueue.h * Author: * * Created on 2 October, 2014, 8:08 AM */ #include <iostream> #include <cmath> #ifndef CQUEUE_H #define CQUEUE_H class cqueue { private : int *arr ,idx; int ARR_LEN; public : int init(int len); void insert ( int item ) ; int mean( ) ; } ; #endif /* CQUEUE_H */ 

and a source file cqueue.cpp

 #include <iostream> #include <cmath> #include "cqueue.h" using namespace std; int cqueue :: init( int maxsize ){ arr = new int [ maxsize ]; for (int i = 0; i < ARR_LEN; i++) { arr[i]=0; } ARR_LEN=maxsize; idx=0; return 1; } void cqueue :: insert(int value) { arr[idx] = value; if (idx == ARR_LEN - 1) idx = 0; else idx++; } int cqueue :: mean() { int ret=0; for (int i=0;i<ARR_LEN;i++) ret=ret+arr[i]; return ret/ARR_LEN; } 

I build the so with following code

 g++ -fpic -g -c -Wall cqueue.cpp g++ -shared -Wl-Wl,-soname,cqueue.so -o cqueue.so cqueue.o 

After that I wrote a sample code to load that so

 #include <iostream> #include <cmath> #include "cqueue.h" using namespace std; int main(void) { cqueue test; test.init(2000); int a; for (int i = 0; i < 100000; i++) { test.insert(i); a=test.mean(); } cout<<a<<endl; return 0; } 

When I tried to compile it, i am getting following error

 g++ -Wall -I . -L . sotest.cpp -o test -lcqueue /usr/bin/ld: cannot find -lcqueue collect2: ld returned 1 exit status 

All codes are in the same folder. What could be wrong here? I tried to export LD_LIBRARY_PATH=/home/current/folder but no luck.

Any help would be appreciated.

1
  • I think no space between -L and . so -L. rather than -L .. (same with -I). Commented Oct 2, 2014 at 3:45

1 Answer 1

4

rename cqueue.so to libcqueue.so.

-l assumes library name start with lib

Sign up to request clarification or add additional context in comments.

1 Comment

opps.. How can I missed that. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.