0

I have a bunch of C++ files that reference functions defined in a sub-directory inside src/. Is there a way to specify the cpp files inside the sub-directory via g++?

Here is the structure of the package:

# SeqLib
#   |----src
#   |-----|---Makevars 
#   |-----|---rcpp_hello_world.cpp
#   |-----|---SeqLib(a submodule)
#   |-----|------|---SeqLib 
#   |-----|------|---FermiAssembler.h
#   |-----|------|---src 
#   |-----|------|---|----FermiAssembler.cpp

************************* EDIT **************

When running -I../src/SeqLib/, I get an error undefined symbol: _ZN6SeqLib14FermiAssemblerD1Ev. Using c++filt, the symbol references the destructor declared in FermiAssembler.h but, defined in FermiAssembler.cpp

6
  • for the includes you have -I option. For the code, you have the linker... Commented Nov 10, 2016 at 20:52
  • Thanks for the reply. If I specify the includes with -I could I just reference FermiAssembler in rcpp_hello_world.cpp via #include "SeqLib/FermiAssembler.h" or "SeqLib/FermiAssembler.cpp" Commented Nov 10, 2016 at 20:54
  • don't include other .cpp files! and with your structure you don't need option if you pass the correct relative path in the include directive. Commented Nov 10, 2016 at 20:55
  • for some reason, the destructor defined in FermiAssembler.cpp isn't being found. I don't think g++ is able to find FermiAssembler.cpp at all. Commented Nov 10, 2016 at 21:05
  • as I said, you have to compile your .cpp files separately, then link them. undefined symbol means that at least the compilation worked. The link failed :) Commented Nov 10, 2016 at 21:07

1 Answer 1

3

You have to pass all the .cpp files to the compiler command. It's strongly discouraged to include .cpp files in other .cpp files.

The command line that is likely to work for you is:

g++ -ISeqLib -o my_executable rcpp_hello_world.cpp SeqLib/src/FermiAssembler.cpp

If you have a lot of files, it is advised to create a makefile script to avoid recompiling all the code every time.

if your FermiAssembler product contains Makefile.am/in files, it can build itself using a configure script which is probably here. The general idea is the:

cd SeqLib
./configure
make

If it's a library product, it builds as a .a or .so file. In that case, the command line becomes:

g++ -ISeqLib -o my_executable rcpp_hello_world.cpp SeqLib/bin/FermiAssembler.a

(I'm just gessing the path & name of the output library file)

To include .a files from there, just add their path. Not sure that SeqLib/bin/*.a would do it because lib dependencies don't follow alphabetical order. A very bruteforce thing that would work would be specifying all .a files twice (so inter-lib dependencies would work):

g++ -ISeqLib -o my_executable rcpp_hello_world.cpp SeqLib/bin/*.a SeqLib/bin/*.a

it would be better, though, to include only the required .a files, respecting the following order: first the ones depending on the following ones. The last library mustn't depend of any of the previous ones.

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

3 Comments

gotcha. Will try it out. There are about 10 cpp files but, interestingly the submodule has its own Makefile.am and Makefile.in. Is it possible to call them without running those 10 cpp files through g++. I'm guessing no because that'll just compile the code but, not make it referencable to rcpp_hello_world.cpp
what is my_executable? After running ./configure and make a bin/ folder was outputted with multiple .a files. I specified the path to them using -L../src/SeqLib/bin/ Is that the correct way of specifying them?
my_executable is your executable (name it as you like). the configure command has worked! good. Now you can check my edit. You could also use -L to specify lib path then -lname to reference libname.a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.