kgui
kgui

Reputation: 4165

Specify source file that defines function previously declared in a header file

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

Upvotes: 0

Views: 112

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

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.

Upvotes: 3

Related Questions