1
votes

I am forking an existing project on Github that I want to make some changes to. One thing I want to do is add an extra file. This file should be in one of the libraries that the Makefile.am generates. The problem is that the file I want to add is a .c file, while everything else in the project is .cpp.

The library that should contain the file is used like this in the makefile:

MYLIBRARY=path/mylibrary.a

...

path_mylibrary_a_CPPFLAGS = $(AM_CPPFLAGS)
path_mylibrary_a_CXXFLAGS = $(AM_CXXFLAGS)
path_mylibrary_a_SOURCES = \
  path/cppfile1.cpp \
  path/cppfile1.h \
  path/cppfile2.cpp \
  path/cppfile2.h \
  path/cppfile3.cpp \
  path/cppfile3.h

...

mybinary_LDADD = $(MYLIBRARY)

Simply adding the path/cfile.c and path/cfile.h to the list of sources gives me the following error:

CXXLD    mybinary
/usr/bin/ld: path/mylibrary.a(path_mylibrary_a-cfile.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
path/mylibrary.a: error adding symbols: Bad value

What can I do so the Makefile.am will compile the c file in a project that otherwise is built in c++?

2
Maybe you should add a path_mylibrary_a_CFLAGS variable? Suitably initialized of course.Some programmer dude

2 Answers

0
votes

The canonical way to solve this problem is to have a convenience library in which you compile your c code:

path_mylibrary_a_LDADD = cfile.la

noinst_LTLIBRARIES = cfile.la
cfile_la_CPPFLAGS = -std=c11
cfile_la_SOURCES = cfile.c

For more information refer to this answer.

0
votes

I presume your cfile.h has the structure

#ifndef MYLIBRARY_CFILE_H
#define MYLIBRARY_CFILE_H

#ifdef __cplusplus
extern "C" {
#endif

/* your C declarations here */

#ifdef __cplusplus
}
#endif

#endif /* MYLIBRARY_CFILE_H */

Also, as a general tip: If you want to add a few source files to a library or program in a Makefile.am file, just add the lines

path_mylibrary_a_SOURCES += cfile.c cfile.h

which makes for a very clean patch which only adds one line, does not touch other lines, etc.

Also, I concur with https://stackoverflow.com/users/440558/some-programmer-dude on the path_mylibrary_a_CFLAGS which you probably need to add as well.