Building and linking lua from source via makefile - lua

I've tried to compile and link lua with my application. This is my makefile:
app.o: app.cpp
g++ -c app.cpp
PATH_LUA=../lua-5.4.3/src
LUA= \
$(PATH_LUA)/lapi.c \
$(PATH_LUA)/lauxlib.c \
$(PATH_LUA)/lbaselib.c \
$(PATH_LUA)/lcode.c \
$(PATH_LUA)/lcorolib.c \
$(PATH_LUA)/lctype.c \
$(PATH_LUA)/ldblib.c \
$(PATH_LUA)/ldebug.c \
$(PATH_LUA)/ldo.c \
$(PATH_LUA)/ldump.c \
$(PATH_LUA)/lfunc.c \
$(PATH_LUA)/lgc.c \
$(PATH_LUA)/linit.c \
$(PATH_LUA)/liolib.c \
$(PATH_LUA)/llex.c \
$(PATH_LUA)/lmathlib.c \
$(PATH_LUA)/lmem.c \
$(PATH_LUA)/loadlib.c \
$(PATH_LUA)/lobject.c \
$(PATH_LUA)/lopcodes.c \
$(PATH_LUA)/loslib.c \
$(PATH_LUA)/lparser.c \
$(PATH_LUA)/lstate.c \
$(PATH_LUA)/lstring.c \
$(PATH_LUA)/lstrlib.c \
$(PATH_LUA)/ltable.c \
$(PATH_LUA)/ltablib.c \
$(PATH_LUA)/ltm.c \
$(PATH_LUA)/lundump.c \
$(PATH_LUA)/lutf8lib.c \
$(PATH_LUA)/lvm.c \
$(PATH_LUA)/lzio.c
libLua.so:
g++ -shared $(LUA) -fPIC -o lua/libLua.so
all: libLua.so app.o
g++ -o app app.o -I$(PATH_LUA) -lLua -Llua -ldl
The compilation works fine, but something is wrong with the linkage, because I get tons of undefined references to "lua_XYZ-function". The undefined reference to lua_tointegerx for example makes no sense, since its implementation is located in lapi.c which is in the file-list above.
(INB4: "This is a duplicate question!" … I've tried my best to check them all.)
What am I missing?

You are putting the libLua.so (by the way, personally I think it's a really bad idea to name your library the same name as the standard library, just uppercase... there's no way that can't bite you or someone in the rear at some point) in the lua subdirectory but you don't tell the linker where to find it.
Then, you list liblua.a as the prerequisite, but you have no actual rule to build liblua.a, which means make will come up with one based on its internal rules... whatever rule it uses will certainly not include all your source files (how could it know?).
If you show us the actual output of running make (before all the error messages) it should be pretty clear this has gone wrong from the beginning.
Speaking from a strictly makefile perspective, it's virtually always wrong to have the name of the file your recipe builds be different than the name of the target in the makefile.

You have to compile lua as c++-code explicitly.
You do this by including
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
instead of
#include <lua.hpp> which includes that extern "C" {...} statement.

Related

java_toolchain: how can I use the Eclipse compiler in a Bazel build

I want to migrate an ant build to Bazel 4.2.1.
The ant build uses an Eclipse compiler (ecj-3.27.0).
The way to declare a Java compiler in Bazel is java_toolchain.
So I had a look at the output of bazel query #bazel_tools//tools/jdk:all and tried to use the vanilla toolchain as an inspiration (bazelisk query --output=build #bazel_tools//tools/jdk:toolchain_vanilla).
java_toolchain(
# modified settings
name = "my-ecj-toolchain",
javabuilder = ["#ecj//:ecj.jar"], # my Eclipse compiler jar
target_version = "8",
# prevent ecj.jar's error message 'unknown option --persistent_workers'
javac_supports_workers = False,
# prevent ecj.jar's error message 'unknown option --persistent_workers'
javac_supports_multiplex_workers = False,
# keeping most vanilla-toolchain settings
bootclasspath = ["#bazel_tools//tools/jdk:platformclasspath"],
misc = ["-XDskipDuplicateBridges=true", "-XDcompilePolicy=simple", "-g", "-parameters"],
jvm_opts = [],
tools = ["#bazel_tools//tools/jdk:javac_jar", "#bazel_tools//tools/jdk:java_compiler_jar", "#bazel_tools//tools/jdk:jdk_compiler_jar"],
singlejar = ["#bazel_tools//tools/jdk:singlejar"],
forcibly_disable_header_compilation = True,
genclass = ["#bazel_tools//tools/jdk:genclass"],
ijar = ["#bazel_tools//tools/jdk:ijar"],
header_compiler = ["#bazel_tools//tools/jdk:turbine_direct"],
header_compiler_direct = ["#bazel_tools//tools/jdk:turbine_direct"],
jacocorunner = "#bazel_tools//tools/jdk:JacocoCoverageFilegroup",
)
However, it still does not work. ecj.jar compiles output the unknown option --output.
bazel aquery MYTARGET displays the whole compiler command line (plus some more build steps):
Command Line: (exec external/remotejdk11_linux/bin/java \
-jar \
external/ecj/ecj.jar \
--output \
bazel-out/k8-opt/bin/[...].jar \
--native_header_output \
bazel-out/k8-opt/bin/[...]-native-header.jar \
--output_manifest_proto \
bazel-out/k8-opt/bin/[...].jar_manifest_proto \
--compress_jar \
--output_deps_proto \
bazel-out/k8-opt/bin/[...].jdeps \
--bootclasspath \
bazel-out/k8-opt/bin/external/bazel_tools/tools/jdk/platformclasspath.jar \
--sources \
bazel-out/k8-opt/bin/[...].java \
--javacopts \
-target \
8 \
'-XDskipDuplicateBridges=true' \
'-XDcompilePolicy=simple' \
-g \
-parameters \
-g \
-- \
--target_label \
bazel-out/k8-opt/bin/[...]:[...] \
--strict_java_deps \
ERROR \
--direct_dependencies \
[...]
I don't know any Java compiler that accepts --output. Do I have to pass ecj.jar in a different way?
I don't know any Java compiler that accepts --output.
Indeed, Bazel does not invoke OpenJDK's javac executable directly but rather its own wrapper over javac's APIs called buildjar. There is a little about this in the Bazel documentation; buildjar enables some of Bazel's fancier Java features.
So, you would have to provide a buildjar-compatible wrapper for EJC. The closest starting point for such an implementation is likely VanillaJavaBuilder, which is for OpenJDK's javac without any of the invasive API usages in the normal buildjar implementation.

How to cross-compile Coreutils or other GNU projects with clang/LLVM?

I have a tough need to compile Coreutils with llvm for other arch: arm/aarch64/mips/mips32/ppc/ppc32...
Since I install all the gcc-cross tools like mips-linux-gnu, powerpc64-linux-gnu and if I have a simple C program like that test.c
#include<stdio.h>
int main(){
printf("hello!");
return 0;
}
I can compile it to the arch, i.e.
clang --target=mips64-linux-gnuabi64 test.c -o test-mips64
➜ tests file test-mips64
test-mips64: ELF 64-bit MSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, BuildID[sha1]=7b33d55a0d08e6cd18d966341590dc351e346a78, for GNU/Linux 3.2.0, not stripped
I try to the same way for compile Coreutils that try to set
export CC=clang
export CXX=clang++
CFLAGS = "--target=mips64-linux-gnuabi64"
./configure --host=mips64-linux-gnuabi64
Howerver, every time got errors in configure or make...
How should I set the configure? Can I easily compile Coreuntils with llvm for other archs?
It's a bit tricky to get the command-line options right for cross-compiling. I got it to work with the commands below, assuming you're working on a Debian-based system (like Debian or Ubuntu). Here are the steps.
Install gcc-mips64-linux-gnuabi64 and gcc-powerpc64-linux-gnu.
Choose the correct arguments for CFLAGS
-B/usr/mips64-linux-gnuabi64/bin/ to indicate we want to use the linker ld within that directory. Do the same for powerpc.
--target=mips64-linux-gnuabi64 to indicate what our target for compilation is. Do the same for powerpc.
-I/usr/mips64-linux-gnuabi64/include to include header files. Do the same for powerpc.
Use ./configure --host=mips64-linux-gnuabi to configure for mips64 and ./configure --host=powerpc64-linux-gnueabi to configure for powerpc64.
Here are the commands to compile for mips64:
make clean
CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
./configure --host=mips64-linux-gnuabi
make
And the commands to compile for powerpc64:
make clean
CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
./configure --host=powerpc64-linux-gnueabi
make
Here is the output of file ./src/ls to demonstrate that it is a powerpc64 executable:
$ file ./src/ls
./src/ls: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, interpreter /lib64/ld64.so.1, for GNU/Linux 3.2.0, BuildID[sha1]=97fe33981ca0112160f44a6fb678d6dc1b462114, not stripped
Below is a Dockerfile that can be used to reproducibly cross-compile coreutils for mips64 and powerpc64.
# Cross-compile GNU coreutils for mips64 and powerpc64 using clang.
# With help from https://medium.com/#wolfv/cross-compiling-arm-on-travis-using-clang-and-qemu-2b9702d7c6f3
FROM debian:buster
# Install compile-time dependencies.
RUN apt-get update \
&& apt-get install --yes \
clang \
curl \
gcc-mips64-linux-gnuabi64 \
gcc-powerpc64-linux-gnu \
make \
perl \
&& rm -rf /var/lib/apt/lists/*
# Download source code for release.
WORKDIR /tmp/coreutils
RUN curl -fsSL https://ftp.gnu.org/gnu/coreutils/coreutils-8.32.tar.xz \
| tar xJ --strip-components 1
# Compile and install for mips64.
RUN CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
./configure --host=mips64-linux-gnuabi --prefix=/opt/coreutils-mips \
&& make \
&& make install
# Compile and install for powerpc64.
RUN make clean \
&& CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
./configure --host=powerpc64-linux-gnueabi --prefix=/opt/coreutils-powerpc64 \
&& make \
&& make install
# Keep only the compiled programs from the previous stage.
FROM debian:buster
COPY --from=0 /opt /opt
I am current working on a simple build tool in Python that maybe help you.
Unfortunately, still at moment, lacks clang implementation, but works fine with GCC and MSVC.
Basically the thing mix Json parameters files to generate command line building.
CppMagic

Linking Boost static libraries

I am trying to compile a shared library using static libraries from Boost and OpenCV. Here below the command I am using to compile my library.
g++ -fPIC libsaliency.cpp -shared -o libsaliency.so \
-I/home/poiesi/data/libraries/boost_1_66_0/installed_w_contrib_static/include -I/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/include \
-Wl,--whole-archive \
/home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a \
/home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_filesystem.a \
/home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_system.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_core.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_highgui.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_imgproc.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_imgcodecs.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_features2d.a \
/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_video.a \
-Wl,--no-whole-archive
However, I have this error:
usr/bin/ld: /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a(read_graphviz_new.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a(read_graphviz_new.o): error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Makefile:7: recipe for target 'saliency' failed
make: *** [saliency] Error 1
Does this mean I have to recompile Boost using -fPIC command? I checked this online but I haven't found much info about it. This makes me wonder if I am searching for the right thing. Do you have any suggestion?
EDIT: As suggested below by Mike, I recompiled Boost like this:
./b2 cxxflags="-fPIC" link=static install
and I can now compile my .so library.
Does this mean I have to recompile Boost using -fPIC command?
Yes. All code that is linked into a shared library must be Position Independent Code. Object
files within static libraries normally are not, as shared libraries normally
link other shared libraries.
But there is nothing in principle to stop you from building boost static libraries
from -fPIC-compiled object files.
It would be simpler, of course, to link the shared versions of the boost libraries.
With boost 1.72.0, fixed this problem by recompile boost static library with -fPIC.
./bootstrap.sh --prefix=/usr/
sudo ./b2 cxxflags=-fPIC cflags=-fPIC link=static -a
sudo ./b2 install

openWRT Makefile for python backports-abc

I'm attempting to build the backports-abc python module in to my OpenWRT build, and need a bit of help on the build/extraction process.
My issue seems to be that the downloaded backports-abc tar file extracts the content to backports_abc-0.5 due to the underlying tar file structure, but the build process is looking for the setup.py etc under backports-abc-0.5 (see below). I can manually copy the files over and rebuild, and that works fine, but that's not ideal. Also, though it builds OK, when I load the image to the device, the backports module is missing.
/home/ubuntu/build2/openwrt/staging_dir/host/bin/python2: can't open file './setup.py': [Errno 2] No such file or directory
Makefile:59: recipe for target '/home/ubuntu/build2/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/backports-abc-0.5//.built' failed
make[3]: *** [/home/ubuntu/build2/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/backports-abc-0.5//.built] Error 2
make[3]: Leaving directory '/home/ubuntu/build2/openwrt/feeds/linkit/python-backports-abc
'
The openWRT documentation around Makefiles is a bit vague, and so is my understanding of the process.
How do I get the tar file extracted to the right folder, or where in the Makefile process should I just copy the files to a correct folder?
Here's my Makefile.
include $(TOPDIR)/rules.mk
PKG_NAME:=backports_abc
PKG_VERSION:=0.5
PKG_RELEASE:=1
PKG_SOURCE:=backports_abc-0.5.tar.gz
PKG_SOURCE_URL:=https://pypi.python.org/packages/68/3c/1317a9113c377d1e33711ca8de1e80afbaf4a3c950dd0edfaf61f9bfe6d8/
PKG_MD5SUM:=7d1936ec183a3586290adf60f6f96764
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)/
#PKG_BUILD_DEPENDS:=python-setuptools
include $(INCLUDE_DIR)/package.mk
$(call include_mk, python-package.mk)
define Package/backports_abc
SECTION:=lang-python
CATEGORY:=Languages
SUBMENU:=Python
TITLE:=backports_abc
URL:=https://pypi.python.org/pypi/backports_abc/0.5
DEPENDS:=+python
endef
define Package/backports_abc/description
A backport of recent additions to the 'collections.abc' module.
endef
define Build/Compile
$(call Build/Compile/PyMod,., \
install --prefix="/usr" --root="$(PKG_INSTALL_DIR)" \
)
endef
define Build/InstallDev
$(INSTALL_DIR) $(STAGING_DIR)$(PYTHON_PKG_DIR)
$(CP) \
$(PKG_INSTALL_DIR)$(PYTHON_PKG_DIR)/* \
$(STAGING_DIR)$(PYTHON_PKG_DIR)/
[ ! -e $(PKG_INSTALL_DIR)/usr/include ] || $(CP) \
$(PKG_INSTALL_DIR)/usr/include/* \
$(STAGING_DIR)/usr/include/
endef
define Package/backportsabc/install
$(INSTALL_DIR) $(1)$(PYTHON_PKG_DIR)/
$(CP) \
$(PKG_INSTALL_DIR)$(PYTHON_PKG_DIR)/* \
$(1)$(PYTHON_PKG_DIR)/
endef
$(eval $(call BuildPackage,backports_abc))
Copy your Makefile somewhere in packages directory, and to overcome the issue remove PKG_BUILD_DIR variable from your Makefile.
I resolved this by fixing up some underscores and hyphens... ie: backports_abc and backports-abc
Basically..
This... PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)/
meant that it was looking for the files based on the PKG_NAME variable.
include $(TOPDIR)/rules.mk
PKG_NAME:=backports_abc
PKG_VERSION:=0.5
PKG_RELEASE:=1
PKG_SOURCE:=backports_abc-0.5.tar.gz
PKG_SOURCE_URL:=https://pypi.python.org/packages/68/3c/1317a9113c377d1e33711ca8de1e80afbaf4a3c950dd0edfaf61f9bfe6d8/
PKG_MD5SUM:=7d1936ec183a3586290adf60f6f96764
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)/
#PKG_BUILD_DEPENDS:=python-setuptools
include $(INCLUDE_DIR)/package.mk
$(call include_mk, python-package.mk)
define Package/backports-abc
SECTION:=lang-python
CATEGORY:=Languages
SUBMENU:=Python
TITLE:=backports-abc
URL:=https://pypi.python.org/pypi/backports_abc/0.5
DEPENDS:=+python
endef
define Package/backports-abc/description
A backport of recent additions to the 'collections.abc' module.
endef
define Build/Compile
$(call Build/Compile/PyMod,., \
install --prefix="/usr" --root="$(PKG_INSTALL_DIR)" \
)
endef
define Build/InstallDev
$(INSTALL_DIR) $(STAGING_DIR)$(PYTHON_PKG_DIR)
$(CP) \
$(PKG_INSTALL_DIR)$(PYTHON_PKG_DIR)/* \
$(STAGING_DIR)$(PYTHON_PKG_DIR)/
[ ! -e $(PKG_INSTALL_DIR)/usr/include ] || $(CP) \
$(PKG_INSTALL_DIR)/usr/include/* \
$(STAGING_DIR)/usr/include/
endef
define Package/backports-abc/install
$(INSTALL_DIR) $(1)$(PYTHON_PKG_DIR)/
$(CP) \
$(PKG_INSTALL_DIR)$(PYTHON_PKG_DIR)/* \
$(1)$(PYTHON_PKG_DIR)/
endef
$(eval $(call BuildPackage,backports-abc))

FFMPEG iOS 7 Library

I've tried reading many tutorials.
I've spent hours on google, and stackoverflow trying answer.
So far I've read: Trying to compile the FFMPEG libraries for iPhoneOS platform with armv6 and arv7 architecture FFMPEG integration on iphone/ ipad project and https://github.com/lajos/iFrameExtractor few of the many.
I'm trying to build this library for iOS 7/Xcode 5 compatibility but it's not working.
A common error I'd get is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
yasm/nasm not found or too old. Use --disable-yasm for a crippled build.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user#ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.log" produced by configure as this will help
solving the problem.
I'd also get many more once that is finished. Such as:
rm: illegal option -- .
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
make: *** [clean] Error 64
I've mostly tried using this command to start, but it always crashes on "make clean":
./configure \
--cc=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc \
--as='/usr/local/bin/gas-preprocessor.pl /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc' \
--sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk \
--target-os=darwin \
--arch=arm \
--cpu=cortex-a8 \
--extra-cflags='-arch armv7' \
--extra-ldflags='-arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk' \
--prefix=compiled/armv7 \
--enable-cross-compile \
--enable-nonfree \
--enable-gpl \
--disable-armv5te \
--disable-swscale-alpha \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-asm \
--disable-debug
To use the mooncatventures (our) ffmpegdecoderFramework, go into build settings
click on the arch and remove the armv7s.
you must also change build active architechure only to no.
This answer from mientus worked for me :
https://stackoverflow.com/a/19370679/661720
Support universal ffmpeg library for iOS7 and XCode5:
Install gas-preprocessor
Click on the ZIP icon to download
https://github.com/mansr/gas-preprocessor. Copy gas-preprocessor.pl to
/usr/bin directory. Change permission of gas-preprocessor.pl by
setting the privilege to Read & Write for all.
Download my shell script from: https://gist.github.com/m1entus/6983547
Run sh build-ffmpeg.sh.

Resources