user72866: I need some help with adding files to the code base by adding them to the autotools build system. Other posts that try to answer the same question haven't helped me. I am experimenting with Bitcoin's codebase and I am trying to add new files to the codebase. I am trying to add a static library in the src/Makefile.am by adding these lines: 
LIBGLYPH=libglyph.a

libglyph_a_SOURCES = \
glyph/glp.c \
  glyph/glp_utils.c \
  glyph/glp_rand.c \
  glyph/glp_rand_openssl_aes.c \
  glyph/FFT/FFT_1024_65537.c \
  glyph/converter.c
libglyph_a_CPPFLAGS = $(AM_CPPFLAGS)
libglyph_a_CFLAGS =
libglyph_a_CXXFLAGS = $(AM_CXXFLAGS)

    EXTRA_LIBRARIES += \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_COMMON) \
  $(LIBGLYPH) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_SERVER) \
  $(LIBBITCOIN_CLI) \
  $(LIBBITCOIN_WALLET) \
  $(LIBBITCOIN_ZMQ)

bitcoin_tx_LDADD = \
  $(LIBUNIVALUE) \
  $(LIBBITCOIN_COMMON) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBGLYPH) \
  $(LIBSECP256K1)

bitcoind_LDADD = \
  $(LIBBITCOIN_SERVER) \
  $(LIBBITCOIN_COMMON) \
  $(LIBUNIVALUE) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_WALLET) \
  $(LIBBITCOIN_ZMQ) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBLEVELDB) \
  $(LIBLEVELDB_SSE42) \
  $(LIBMEMENV) \
  $(LIBGLYPH) \
  $(LIBSECP256K1)

I have tried at least 30 different compilation attempts where I reordered the compilation and/or renamed the library. 
Error:
make[3]: Entering directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/secp256k1'
make[3]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/secp256k1'
  CXXLD    bitcoind
libbitcoin_common.a(libbitcoin_common_a-key.o): In function `CKey::MakeNewKey()':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:25: undefined reference to `glp_gen_sk(glp_signing_key_st*)'
libbitcoin_common.a(libbitcoin_common_a-key.o): In function `CKey::GetPubKey() const':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:48: undefined reference to `glp_gen_pk(glp_public_key_st*, glp_signing_key_st)'
collect2: error: ld returned 1 exit status
Makefile:3556: recipe for target 'bitcoind' failed
make[2]: *** [bitcoind] Error 1
make[2]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src'
Makefile:9207: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src'
Makefile:670: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1

This error occurs despite libglyph.a being made and containing the functions. I have tried to add the library header and C files to common library with no luck either. I have rearranged the EXTRA_LIBRARIES section of the Makefile.am with no luck.

In order to simplify the problem, here is a really simple example that spits out the same error:
Adding 1 .h and 1 .cpp file to 1 already made library: libbitcoin_common
new files: src/testFile.cpp src/testFile.h
testFile.h:
void testFunction();

testFile.cpp
#include "testFile.h"
#include <stdio.h>

void testFunction() {
    printf("I am a test function");
}

added following lines to key.cpp: 
#include "testFile.h"

void CKey::MakeNewKey()
{
    testFunction();
     ...
     ...
     ...
}

Then in src/Makefile.am
BITCOIN_CORE_H = \
testFile.h \
...
...

and the testFile to the common library, where key.cpp is, as mentioned by Pieter Wuille here
# common: shared between bitcoind, and bitcoin-qt and non-server tools
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_common_a_SOURCES = \
  amount.cpp \
  base58.cpp \
  chainparams.cpp \
  coins.cpp \
  compressor.cpp \
  core_read.cpp \
  core_write.cpp \
  keystore.cpp \
  netaddress.cpp \
  netbase.cpp \
  protocol.cpp \
  scheduler.cpp \
  script/sign.cpp \
  script/standard.cpp \
  key.cpp \
  testFile.cpp \
  $(BITCOIN_CORE_H)

The error:
libbitcoin_common.a(libbitcoin_common_a-key.o): In function 
`CKey::MakeNewKey()':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:23: undefined reference to `testFunction()

Please help me out with this. Thanks a lot

user48884: Adding a single source file and header is easier to do than adding a new library.
For a single source file, the file must come before any file that depends on it. So in your example, you should have
...
testFile.cpp \
key.cpp \
...

But you are really trying to add a new library. To do that, you need to add it to bitcoind_LDADD which is further down in Makefile.am. That way the linker knows to link it. For the linker, the dependencies need to be placed after the things that depend on it. So you can just adding $(LIBGLYPH) to the end of that list should work.

user72866: There were 2 problems with the code: 

you cannot compile c code with a c++ compiler without extern "C" in the header files of the C code. https://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs
-fPIC might have to be used when compiling the C code (add it to the flags). I used liblibraryName_a_CFLAGS = -fPIC and liblibraryName_a_CXXFLAGS = -fPIC 

For anyone that wants to see my makefile.am, go here: https://drive.google.com/file/d/1eB_EnxfSG1erRgDpDNgU_q-PzPQYs0hp/view?usp=sharing
Build systems are notoriously frustrating. If you have questions, leave a comment or ask me.

72866: <p>I need some help with adding files to the code base by adding them to the autotools build system. Other <a href="https://bitcoin.stackexchange.com/questions/53957/how-to-add-own-cpp-files-to-makefile">posts</a> that try to answer the same question haven't helped me. I am experimenting with Bitcoin's codebase and I am trying to add new files to the codebase. I am trying to add a static library in the src/Makefile.am by adding these lines: </p>

<pre><code>LIBGLYPH=libglyph.a

libglyph_a_SOURCES = \
glyph/glp.c \
  glyph/glp_utils.c \
  glyph/glp_rand.c \
  glyph/glp_rand_openssl_aes.c \
  glyph/FFT/FFT_1024_65537.c \
  glyph/converter.c
libglyph_a_CPPFLAGS = $(AM_CPPFLAGS)
libglyph_a_CFLAGS =
libglyph_a_CXXFLAGS = $(AM_CXXFLAGS)

    EXTRA_LIBRARIES += \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_COMMON) \
  $(LIBGLYPH) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_SERVER) \
  $(LIBBITCOIN_CLI) \
  $(LIBBITCOIN_WALLET) \
  $(LIBBITCOIN_ZMQ)

bitcoin_tx_LDADD = \
  $(LIBUNIVALUE) \
  $(LIBBITCOIN_COMMON) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBGLYPH) \
  $(LIBSECP256K1)

bitcoind_LDADD = \
  $(LIBBITCOIN_SERVER) \
  $(LIBBITCOIN_COMMON) \
  $(LIBUNIVALUE) \
  $(LIBBITCOIN_UTIL) \
  $(LIBBITCOIN_WALLET) \
  $(LIBBITCOIN_ZMQ) \
  $(LIBBITCOIN_CONSENSUS) \
  $(LIBBITCOIN_CRYPTO) \
  $(LIBLEVELDB) \
  $(LIBLEVELDB_SSE42) \
  $(LIBMEMENV) \
  $(LIBGLYPH) \
  $(LIBSECP256K1)
</code></pre>

<p><strong>I have tried at least 30 different compilation attempts where I reordered the compilation and/or renamed the library.</strong> </p>

<p>Error:</p>

<pre><code>make[3]: Entering directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/secp256k1'
make[3]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/secp256k1'
  CXXLD    bitcoind
libbitcoin_common.a(libbitcoin_common_a-key.o): In function `CKey::MakeNewKey()':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:25: undefined reference to `glp_gen_sk(glp_signing_key_st*)'
libbitcoin_common.a(libbitcoin_common_a-key.o): In function `CKey::GetPubKey() const':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:48: undefined reference to `glp_gen_pk(glp_public_key_st*, glp_signing_key_st)'
collect2: error: ld returned 1 exit status
Makefile:3556: recipe for target 'bitcoind' failed
make[2]: *** [bitcoind] Error 1
make[2]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src'
Makefile:9207: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src'
Makefile:670: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1
</code></pre>

<p>This error occurs despite libglyph.a being made and containing the functions. I have tried to add the library header and C files to common library with no luck either. I have rearranged the EXTRA_LIBRARIES section of the Makefile.am with no luck.</p>

<hr>

<p>In order to simplify the problem, here is a really simple example that spits out the same error:</p>

<p>Adding 1 .h and 1 .cpp file to 1 already made library: libbitcoin_common
new files: <code>src/testFile.cpp src/testFile.h</code></p>

<p>testFile.h:</p>

<pre><code>void testFunction();
</code></pre>

<p>testFile.cpp</p>

<pre><code>#include "testFile.h"
#include &lt;stdio.h&gt;

void testFunction() {
    printf("I am a test function");
}
</code></pre>

<p>added following lines to key.cpp: </p>

<pre><code>#include "testFile.h"

void CKey::MakeNewKey()
{
    testFunction();
     ...
     ...
     ...
}
</code></pre>

<p>Then in src/Makefile.am</p>

<pre><code>BITCOIN_CORE_H = \
testFile.h \
...
...
</code></pre>

<p>and the testFile to the common library, where key.cpp is, as mentioned by Pieter Wuille <a href="https://bitcoin.stackexchange.com/questions/57938/bitcoin-core-makefile-addtions">here</a></p>

<pre><code># common: shared between bitcoind, and bitcoin-qt and non-server tools
libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_common_a_SOURCES = \
  amount.cpp \
  base58.cpp \
  chainparams.cpp \
  coins.cpp \
  compressor.cpp \
  core_read.cpp \
  core_write.cpp \
  keystore.cpp \
  netaddress.cpp \
  netbase.cpp \
  protocol.cpp \
  scheduler.cpp \
  script/sign.cpp \
  script/standard.cpp \
  key.cpp \
  testFile.cpp \
  $(BITCOIN_CORE_H)
</code></pre>

<p>The error:</p>

<pre><code>libbitcoin_common.a(libbitcoin_common_a-key.o): In function 
`CKey::MakeNewKey()':
/home/joe/glyph/glyphCodebase/BitcoinUnlimited/src/key.cpp:23: undefined reference to `testFunction()
</code></pre>

<p>Please help me out with this. Thanks a lot</p>
 48884: <p>Adding a single source file and header is easier to do than adding a new library.</p>

<p>For a single source file, the file must come before any file that depends on it. So in your example, you should have</p>

<pre><code>...
testFile.cpp \
key.cpp \
...
</code></pre>

<p>But you are really trying to add a new library. To do that, you need to add it to <code>bitcoind_LDADD</code> which is further down in <code>Makefile.am</code>. That way the linker knows to link it. For the linker, the dependencies need to be placed after the things that depend on it. So you can just adding <code>$(LIBGLYPH)</code> to the end of that list should work.</p>
 72866: <p>There were 2 problems with the code: </p>

<ol>
<li>you cannot compile c code with a c++ compiler without extern "C" in the header files of the C code. <a href="https://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs">https://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs</a></li>
<li>-fPIC might have to be used when compiling the C code (add it to the flags). I used liblibraryName_a_CFLAGS = -fPIC and liblibraryName_a_CXXFLAGS = -fPIC </li>
</ol>

<p>For anyone that wants to see my makefile.am, go here: <a href="https://drive.google.com/file/d/1eB_EnxfSG1erRgDpDNgU_q-PzPQYs0hp/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1eB_EnxfSG1erRgDpDNgU_q-PzPQYs0hp/view?usp=sharing</a></p>

<p>Build systems are notoriously frustrating. If you have questions, leave a comment or ask me.</p>
