Compile *.cpp and *.c with Automake - c++

Structure:
-myProject
-Makefile.am
-configure.ac
-src
-Makefile.am
-main.c
-test.c
-test.h
When I try to compile, I've got the error Undefinded reference to a function from test.c.
Makefile.am:
AUTOMAKE_OPTIONS = foreign
SUBDIRS=src
src/Makefile.am:
bin_PROGRAMS = TEST
AM_CXXFLAGS =-Wall -Wno-psabi -w -pthread -I/usr/local/include
AM_CFLAGS =-Wall -w
#Sources
TEST_SOURCES = test.c main.cpp
TEST_LDADD = #LDFLAGS# -lmosquitto
TEST_LDFLAGS =-lmosquitto
It seem's, that it's not possible to linkt the two sources. Can anybody help me??
ERROR:
make all-recursive
make[1]: Entering directory '/media/sf_Shared/Software/executables/TEST'
Making all in src
make[2]: Entering directory '/media/sf_Shared/Software/executables/TEST/src'
g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wno-psabi -w -pthread -I/usr/local/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
mv -f .deps/main.Tpo .deps/main.Po
g++ -Wall -Wno-psabi -w -pthread -I/usr/local/include -g -O2 -lmosquitto -o TEST test.o main.o -lmosquitto
/usr/bin/ld: main.o: in function `main':
/media/sf_Shared/Software/executables/TEST/src/main.cpp:564: undefined reference to `open_ports(unsigned int, unsigned int)'
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:366: TEST] Error 1
make[2]: Leaving directory '/media/sf_Shared/Software/executables/TEST/src'
make[1]: *** [Makefile:359: all-recursive] Error 1
make[1]: Leaving directory '/media/sf_Shared/Software/executables/TEST'
make: *** [Makefile:300: all] Error 2
I'm including in the main.cpp the test.h. There is the function open_ports declared(u_int32_t open_ports(u_int32_t a, u_int32_t b);), which is implemented in test.c

This is not an automake issue — you are doing the right thing (or at least close to) in the Makefile.am.
The problem is that your symbol is declared without extern "C", but defined only in C, so when the C++ compiler takes your main.cpp, it expects to find a mangled symbols (which encodes the signature of the function, that is the number and types of parameters), rather than a plain (C) one.
You can tell that this is the case because it's looking for open_ports(unsigned int, unsigned int) — rather than just open_ports.
The easiest solution is to change your main.cpp to say:
extern "C" {
#include "test.h"
}
That declares that the test.h header only includes C declarations, which will then generate the right symbol lookup for what you define in test.c.

Related

Error in in linking makefile (exit code 1)

This is my first time using makefile. I am getting "linker command failed with exit code 1 " error while using "clang".
I just linked .h files in makefile and run make all in ubuntu teminal.
CC_EXEC = clang
CC_FLAGS = -g
CC = ${CC_EXEC} ${CC_FLAGS}
BUILD_DIR = build
all: executable.out
executable.out: ${BUILD_DIR}/main.o ${BUILD_DIR}/UTrelloInterface.o ${BUILD_DIR}/User.o ${BUILD_DIR}/List.o ${BUILD_DIR}/Task.o
clang -g ${BUILD_DIR}/main.o ${BUILD_DIR}/UTrelloInterface.o ${BUILD_DIR}/User.o ${BUILD_DIR}/List.o ${BUILD_DIR}/Task.o -o ${BUILD_DIR}/executable.out
${BUILD_DIR}/main.o: main.cpp UTrelloInterface.h User.h List.h Task.h
${CC} -c main.cpp -o ${BUILD_DIR}/main.o
${BUILD_DIR}/User.o: User.cpp User.h
${CC} -c User.cpp -o ${BUILD_DIR}/User.o
${BUILD_DIR}/List.o: List.cpp List.h
${CC} -c List.cpp -o ${BUILD_DIR}/List.o
${BUILD_DIR}/Task.o: Task.cpp Task.h
${CC} -c Task.cpp -o ${BUILD_DIR}/Task.o
${BUILD_DIR}/UTrelloInterface.o: UTrelloInterface.cpp UTrelloInterface.h Task.h List.h User.h
${CC} -c UTrelloInterface.cpp -o ${BUILD_DIR}/UTrelloInterface.o
.PHONY: clean
clean:
rm -rf build/ && mkdir -p build
I got exactly this error:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
makefile:11: recipe for target 'executable.out' failed
make: *** [executable.out] Error 1
I got this error while using g++ compiler:
g++ -g -c User.cpp -o build/User.o
g++ -g build/main.o build/UTrelloInterface.o build/User.o build/List.o build/Task.o -o build/executable.out
/usr/bin/ld: build/main.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
makefile:11: recipe for target 'executable.out' failed
make: *** [executable.out] Error 1
What should I do?

How do I include a library in an .so file in C++

I am trying to compile a .so file the relies on the FTDI library. I'm not sure how to include the library so that it compiles correctly. (Below is my best guess). Is this possible or is there another way to go about it?
shared: pldevice pldeviceenttecpro pluniverse
$(CC) -shared -Wl,-soname,libplanklight.so -o libplanklight.so\
-L/usr/local/lib -lftd2xx \
pldevice.o pldeviceenttecpro.o pluniverse.o
Edit: This is what the output is:
g++ -fPIC -c pldevice.cpp
g++ -fPIC -c pldeviceenttecpro.cpp
g++ -fPIC -c pluniverse.cpp
g++ -shared -Wl,-soname,libplanklight.so -o libplanklight.so\
-L/usr/local/lib -lftd2xx \
pldevice.o pldeviceenttecpro.o pluniverse.o
/usr/bin/x86_64-linux-gnu-ld: cannot open output file libplanklight.so-L/usr/local/lib: No such file or directory
collect2: error: ld returned 1 exit status
Makefile:5: recipe for target 'shared' failed
make: *** [shared] Error 1
You're missing a space.
$(CC) -shared -Wl,-soname,libplanklight.so -o libplanklight.so \
^^
Add a space here
The \ just makes the command continue on the next line, so when you have
-o libplanklight.so\
-L/usr/local/lib
It will be the same as -o libplanklight.so-L/usr/local/lib
But you want -o libplanklight.so -L/usr/local/lib

Automake & Autoconf - Program Doesn't Recognize Static Library Just Built

I am developing a library called libspellcheck, and a program that uses it to check spelling called spellcheck. Here is my directory structure:
libspellcheck
-> doc
-> man
-> libspellcheck (libspellcheck source)
-> spellcheck (spellcheck source)
I wanted to use a configure script instead of a plain Makefile as I had been using. Everything goes okay until I try compiling spellcheck:
Making all in libspellcheck
make[1]: Entering directory `/home/iandun/libspellcheck-devel/libspellcheck/libspellcheck'
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT checker.o -MD -MP -MF .deps/checker.Tpo -c -o checker.o checker.cpp
mv -f .deps/checker.Tpo .deps/checker.Po
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT SpellCorrector.o -MD -MP -MF .deps/SpellCorrector.Tpo -c -o SpellCorrector.o SpellCorrector.cpp
mv -f .deps/SpellCorrector.Tpo .deps/SpellCorrector.Po
rm -f libspellcheck.a
ar cru libspellcheck.a checker.o SpellCorrector.o
ranlib libspellcheck.a
make[1]: Leaving directory `/home/iandun/libspellcheck-devel/libspellcheck/libspellcheck'
Making all in spellcheck
make[1]: Entering directory `/home/iandun/libspellcheck-devel/libspellcheck/spellcheck'
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT spellcheck.o -MD -MP -MF .deps/spellcheck.Tpo -c -o spellcheck.o spellcheck.cpp
spellcheck.cpp: In function 'int main(int, char**)':
spellcheck.cpp:111:21: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char *dictionary = "/etc/english.dict"; //Default Dictionary
^
spellcheck.cpp:164:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
dictionary = "/etc/english.dict";
^
mv -f .deps/spellcheck.Tpo .deps/spellcheck.Po
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT meta.o -MD -MP -MF .deps/meta.Tpo -c -o meta.o meta.cpp
mv -f .deps/meta.Tpo .deps/meta.Po
g++ -g -O2 "../libspellcheck/libspellcheck.a" -o spellcheck spellcheck.o meta.o
spellcheck.o: In function `correctMisspelled(std::string, std::string)':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:29: undefined reference to `correctSpelling(std::string, std::string)'
spellcheck.o: In function `doFileCheck(char*, char*, char*, spelling)':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:61: undefined reference to `check_spelling_file(char*, char*, std::string)'
spellcheck.o: In function `main':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:193: undefined reference to `add_word(char*, char*)'
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:224: undefined reference to `check_spelling_string(char*, std::string, std::string)'
meta.o: In function `do_about_msg()':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/meta.cpp:29: undefined reference to `lib_version()'
collect2: error: ld returned 1 exit status
make[1]: *** [spellcheck] Error 1
make[1]: Leaving directory `/home/iandun/libspellcheck-devel/libspellcheck/spellcheck'
make: *** [all-recursive] Error 1
I put a reference to libspellcheck.a in my Makefile.am file in the spellcheck directory:
CFLAGS = -m32 -Wall
LDFLAGS = "../libspellcheck/libspellcheck.a"
bin_PROGRAMS = spellcheck
spellcheck_SOURCES = spellcheck.cpp meta.cpp
And also changed my references to the spellcheck header file:
#include "../libspellcheck/spellcheck.h"
Here is my Makefile.am in the libspellcheck folder:
CFLAGS = -m32 -Wall
LDFLAGS =
lib_LIBRARIES = libspellcheck.a
libspellcheck_a_SOURCES = checker.cpp SpellCorrector.cpp
include_HEADERS = spellcheck.h SpellCorrector.h
Here is my Makefile.am in the main folder:
AUTOMAKE_OPTIONS = foreign
SUBDIRS = libspellcheck spellcheck man
And my configure.ac:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(libspellcheck, 1.25, corinthianmonthly#hotmail.com)
AC_OUTPUT(Makefile libspellcheck/Makefile spellcheck/Makefile man/Makefile)
AC_CONFIG_SRCDIR([])
AC_CONFIG_HEADERS([])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h,iostream,fstream,string,stdio.h,sstream,cctype,algorithm,boost/algorithm/string.hpp])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_TYPE_SIZE_T
# Checks for library functions.
AC_OUTPUT
What am I doing wrong?
CFLAGS = -m32 -Wall
LDFLAGS = "../libspellcheck/libspellcheck.a"
First, these are user flags. Use the AM_* forms instead.
Second, LDFLAGS is for flags. It is put near the start of the command line. But, for linking, order matters, so you want to use LDADD instead. There are a few ways to do this but perhaps the best is to use the per-target variable:
spellcheck_LDADD = ../libspellcheck/libspellcheck.a
You don't need those quotes.

undefined reference when linking against libresolv

I have a project that is a library that links against libresolv,
It works fine on recent distros: Ubuntu 10.x Fedora 13, Mandriva
2010.1 but on Centos 5.x I get the following errors
glibc installed is: glibc-2.5-18.el5_1.1
g++ -DHAVE_CONFIG_H -I. -I./include -I/usr/include/postgresql -O3
-ansi -Wall -Wno-deprecated -D_FORTIFY_SOURCE=0 -MT testUpLog.o -MD
-MP -MF .deps/testUpLog.Tpo -c -o testUpLog.o testUpLog.cc
mv -f .deps/testUpLog.Tpo .deps/testUpLog.Po
/bin/sh ./libtool --tag=CXX --mode=link g++ -O3 -ansi -Wall
-Wno-deprecated -D_FORTIFY_SOURCE=0 -L/usr/lib64 -L/lib64
-L/usr/lib64/mysql -o testUpLog testUpLog.o libUpTools.la -lpq
-lmysqlclient -lssl -lpthread
libtool: link: g++ -O3 -ansi -Wall -Wno-deprecated -D_FORTIFY_SOURCE=0
-o .libs/testUpLog testUpLog.o -L/usr/lib64 -L/lib64
-L/usr/lib64/mysql ./.libs/libUpTools.so -lpq -lmysqlclient -lssl
-lpthread
./.libs/libUpTools.so: undefined reference to `__ns_name_uncompress'
./.libs/libUpTools.so: undefined reference to `__ns_initparse'
./.libs/libUpTools.so: undefined reference to `__ns_parserr'
collect2: ld returned 1 exit status
make[1]: *** [testUpLog] Error 1
make[1]: Leaving directory `/tmp/UpTools-8.5.3'
make: *** [check-am] Error 2
library.la file contains:
dlname='libUpTools.so.0'
library_names='libUpTools.so.0.0.0 libUpTools.so.0 libUpTools.so'
old_library='libUpTools.a'
inherited_linker_flags=''
dependency_libs=' -L/usr/lib64 -L/lib64 -L/usr/lib64/mysql -lpq
-lmysqlclient -lssl -lpthread'
weak_library_names=''
current=0
age=0
revision=0
installed=no
shouldnotlink=no
dlopen=''
dlpreopen=''
libdir='/usr/lib'
You can read configure.ac on
http://pastebin.com/hs5q21Rq
Thanks in advance
If libUpTools uses functions lib libresolv, you need to say so:
libUpTools_la_LIBADD = -lresolv (of course -lresolv may be replaced by variables determined by configure etc.)
That way, -lresolv will end up in the .la file and also in the .so file (if you chose to build it) that you can run ldd on for verification.

Error: Compiling simple PjSIP program under ubuntu

I am trying to compile simple PjSIP program under ubuntu. I am getting error as
/usr/bin/ld: cannot find -lpjsua-i686-pc-linux-gnu
What does it mean?
Here is the ouput:-
root#mypc-desktop:/home/mypc/pjsip# make
gcc -o myapp myapp.cpp -DPJ_AUTOCONF=1 -O2 -I/home/mypc/pjproject-1.4.5/pjlib/include -I/home/mypc/pjproject-1.4.5/pjlib-util/include -I/home/mypc/pjproject-1.4.5/pjnath/include -I/home/mypc/pjproject-1.4.5/pjmedia/include -I/home/mypc/pjproject-1.4.5/pjsip/include -L/home/mypc/pjproject-1.4.5/pjlib/lib -L/home/mypc/pjproject-1.4.5/pjlib-util/lib -L/home/mypc/pjproject-1.4.5/pjnath/lib -L/home/mypc/pjproject-1.4.5/pjmedia/lib -L/home/mypc/pjproject-1.4.5/pjsip/lib -L/home/mypc/pjproject-1.4.5/third_party/lib -lpjsua-i686-pc-linux-gnu -lpjsip-ua-i686-pc-linux-gnu -lpjsip-simple-i686-pc-linux-gnu -lpjsip-i686-pc-linux-gnu -lpjmedia-codec-i686-pc-linux-gnu -lpjmedia-i686-pc-linux-gnu -lpjmedia-audiodev-i686-pc-linux-gnu -lpjnath-i686-pc-linux-gnu -lpjlib-util-i686-pc-linux-gnu -lresample-i686-pc-linux-gnu -lmilenage-i686-pc-linux-gnu -lsrtp-i686-pc-linux-gnu -lgsmcodec-i686-pc-linux-gnu -lspeex-i686-pc-linux-gnu -lilbccodec-i686-pc-linux-gnu -lg7221codec-i686-pc-linux-gnu -lportaudio-i686-pc-linux-gnu -lpj-i686-pc-linux-gnu -lm -lnsl -lrt -lpthread
/usr/bin/ld: cannot find -lpjsua-i686-pc-linux-gnu
collect2: ld returned 1 exit status
make: *** [myapp] Error 1
Here is code
#include <pjlib.h>
#include <pjlib-util.h>
#include <pjmedia.h>
#include <pjmedia-codec.h>
#include <pjsip.h>
#include <pjsip_simple.h>
#include <pjsip_ua.h>
#include <pjsua-lib/pjsua.h>
int main()
{
return 0;
}
Here is a Makefile
#Modify this to point to the PJSIP location.
PJBASE=/home/mypc/pjproject-1.4.5
include $(PJBASE)/build.mak
CC = $(APP_CC)
LDFLAGS = $(APP_LDFLAGS)
LDLIBS = $(APP_LDLIBS)
CFLAGS = $(APP_CFLAGS)
CPPFLAGS= ${CFLAGS}
# If your application is in a file named myapp.cpp or myapp.c
# this is the line you will need to build the binary.
all: myapp
myapp: myapp.cpp
$(CC) -o $# $< $(CPPFLAGS) $(LDFLAGS) $(LDLIBS)
clean:
rm -f myapp.o myapp
Thanks
EDIT:
I just noticed that there is a error building PjSIP
make[2]: Entering directory
/home/mypc/pjproject-1.4.5/pjnath/build'
gcc -c -Wall -DPJ_AUTOCONF=1 -O2
-Wno-unused-label -Werror -I../include -I../../pjlib/include -I../../pjlib-util/include \ -o output/pjnath-i686-pc-linux-gnu/stun_session.o
\ ../src/pjnath/stun_session.c cc1:
warnings being treated as errors
../src/pjnath/stun_session.c: In
function ‘apply_msg_options’:
../src/pjnath/stun_session.c:230:
error: suggest parentheses around &&
within || make[2]: ***
[output/pjnath-i686-pc-linux-gnu/stun_session.o]
Error 1 make[2]: Leaving directory
/home/mypc/pjproject-1.4.5/pjnath/build'
make[1]: * [pjnath] Error 2 make[1]:
Leaving directory
`/home/mypc/pjproject-1.4.5/pjnath/build'
make: * [all] Error 1
When I tried to find -Werror in make files present at /home/mypc/pjproject-1.4.5/pjnath/build, $(PJDIR)/build.mak and $(PJDIR)/build/common.mak its not present there anybody know where it could be ?
It seems that the pj* can't build the neccessary libaries - for a simple fix try to locate the line in /home/mypc/pjproject-1.4.5/build.mak where -Werror is added to $(APP_CFLAGS) and remove it (the -Werror, not the whole line if other flags are added :).
Alternatively apply the fix suggested by gcc on line 230 in src/pjnath/stun_session.c.
edit:
Just read that you found no -Werror. Could you either paste line 230 of stun_session.c or the make-files somewhere?
The source line would be preferred.