title: Creating a library file in makefile and compiling after that
body: <p>My problem is fairly easy but I just don't know how to solve it. I know how to compile and library and link against it if I'm not using a make file because then I can just call ar separately and everything goes right.</p>
  
  <p>Anyway I'm using a <a href="http://www.mcs.anl.gov/petsc/" rel="nofollow">petsc</a> library and I'm using a makefile what they provided:</p>
  
  <pre><code>CFLAGS          = 
  FFLAGS          = 
  CPPFLAGS        = 
  FPPFLAGS        =
  LOCDIR          = /home/user/.../.../   # Working folder
  EXAMPLESC       = main.cpp class.cpp        #.cpp file names here
  EXAMPLESF       =
  #MANSEC          = Mat I don't know what this is but it seems to work without it.
  
  include ${PETSC_DIR}/conf/variables
  include ${PETSC_DIR}/conf/rules
  
  myProgram: main.o class.o  chkopts
      -${CLINKER}  -o myProgram main.o class.o ${PETSC_MAT_LIB}
      ${RM} main.o class.o
  
  include ${PETSC_DIR}/conf/test
  </code></pre>
  
  <p>ARFLAGS will be -rv as a default so where should I provide such a information as</p>
  
  <pre><code>ar -rv libclassdll.a class.o
  </code></pre>
  
  <p>and where should I add -L./-lclassdll ?</p>
  
  <p>I'm quite a rookie with makefiles so that's why I'm a bit lost here :&lt;</p>
  
  <p>I tried to change the line to</p>
  
  <pre><code>myProgram: main.o class.o  chkopts
      -${CLINKER}  -o myProgram main.o class.o ${AR} libclassdll.a class.o ${PETSC_MAT_LIB}
      ${RM} main.o class.o
  </code></pre>
  
  <p>and then my compiling command seems to be
  mpicxx -o myProgram main.o class.o /usr/bin/ar/ libclassdll.a class.o -L ( a lot of linking here ) and at least it says: g++ classdll.a no such a file or dir.</p>
  
  <p>So it doesn't generate even a lib file for me. So any ideas will be really appreciated. </p>
  
  <p>A new problem when I uploaded makefile on the different machine, my current makefile looks like this</p>
  
  <pre><code>LibMyClass.so: MyClass.o  chkopts
      -${CLINKER}  -shared -Wl,-soname,${SONAME} -o ${VERS}   *.o  ${PETSC_MAT_LIB}
  
      mv ${VERS} ${LIBADD}
      ln -sf ${LIBADD}${VERS} ${LIBADD}${SOWOV}
      ln -sf ${LIBADD}${VERS} ${LIBADD}${SONAME}
  </code></pre>
  
  <p>That works on one machine but other machine gives following error</p>
  
  <pre><code>/usr/bin/ld: MyClass.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
  MyClass.o: could not read symbols: Bad value
  </code></pre>
  
  <p>I did change the paths of course but I guess that indicates other kind of problem because even if I type "g++ -shared -Wl,-soname,libmyclass.so.1 -o libmyclass.so.1.0   MyClass.o" or "g++ -fPIC -share... " I'll get the same error.</p>