Include dependency for multiple static library - qmake - static

I have some trouble figuring out how qmake handles the include dependencies of a static library that depends on another static library.
My folder structure is as follow :
--- src/
lib1/
src/ (.pro,*.h,*.cpp)
lib2/
src/ (.pro,*.h,*.cpp)
app/
src/ (.pro,*.h,*.cpp)
--- build/
lib1/
lib1.a
lib2/
lib2.a
app/
app
lib2 uses lib1, and app uses lib2. The .pro files are :
lib1.pro
TARGET = lib1
TEMPLATE = lib
CONFIG += staticlib
CONFIG += create_prl link_prl
SOURCES += ...
HEADERS += ...
lib2.pro
TARGET = lib2
TEMPLATE = lib
CONFIG += staticlib
CONFIG += create_prl link_prl
INCLUDEPATH += $${_PRO_FILE_PWD_}/../../lib1/src
DEPENDPATH += $${_PRO_FILE_PWD_}/../../lib1/src
PRE_TARGETDEPS += $${OUT_PWD}/../lib1/liblib1.a
LIBS += -L$${OUT_PWD}/../lib1 -llib1
SOURCES += ...
HEADERS += ...
app.pro
TARGET = app
TEMPLATE = app
CONFIG += link_prl
# Include to lib1 headers ... How to avoid this ?
INCLUDEPATH += $${_PRO_FILE_PWD_}/../../lib1/src
DEPENDPATH += $${_PRO_FILE_PWD_}/../../lib1/src
# lib2
INCLUDEPATH += $${_PRO_FILE_PWD_}/../../lib2/src
DEPENDPATH +=$${_PRO_FILE_PWD_}/../../lib2/src
PRE_TARGETDEPS += $${OUT_PWD}/../lib2/liblib2.a
LIBS += -L$${OUT_PWD}/../lib2 -llib2
SOURCES += ...
HEADERS += ...
Is there a way to avoid the INCLUDEPATH to the lib1 headers in the app.pro ? I thought that the DEPENDPATHin lib2.pro would take care of that, but apparently not.
Edit : Clarifying question :
The problem comes from lib2.h that contains : #include "lib1.h". If I don't include the lib1 headers in app.pro, then I get a compilation error stating that lib1.h is not found. Is this normal ? Not sure if this makes sense, but since app only depends explicitly on lib2 shouldn't it be able to compile only with including lib2 headers ?

It sounds like you need a QMake SUBDIRS Project in the top level directory with the appropriate ".depends" statements. Check out the first answer to "How to Use QMake's SUBDIRS Template and QMake Project Dependencies.

Related

CMake and issues with header files in subfolders

I'm trying to set up a project where header files can be found by the subfolder libraries src code as well as the src code in the top level using Cmake. Currently i am getting an error stating that the header file can not be found. The structure of my project looks like this:
root/
src/
CMakeLists.txt #(top level)
main.c
lib/
lib1.c
CMakeLists.txt #(lower level)
headers/
lib1.h
build/
My top level CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.13.4)
project(CmakeTUT_Proj)
add_executable(${PROJECT_NAME} main.c)
target_include_directories(${PROJECT_NAME} PUBLIC Lib/headers/)
add_subdirectory(Lib/)
target_link_directories(${PROJECT_NAME} PRIVATE Lib/headers/)
target_link_libraries(${PROJECT_NAME} name_of_lib)
My lower level CMakeLists.txt looks like:
add_library(name_of_lib adder.c)
My main.c and my lib1.c programs include the library as #include "lib1.h", cmake runs fine without any errors but when i build the project with make i get an error like:
root/src/Lib/lib1.c:2:10: fatal error: lib1.h: No such file or directory
#include "lib1.h"
I want to structure my project so that main.c and lib1.c have access to lib1.h. Any ideas? Thank you.
As the name implies, target_include_directories only affects one target. So when you set target_include_directories(${PROJECT_NAME} PUBLIC Lib/headers/), this adds to the include path for the executable target named ${PROJECT_NAME} but not to the include path for the library name_of_lib.
To fix this, you can add the include path for your library in the lower level CMakeLists.txt:
target_include_directories(name_of_lib PUBLIC headers)
As a bonus, because it's PUBLIC, this path is also automatically added to any target that depends on name_of_lib. So in the top-level CMakeLists.txt, you can remove this line:
target_include_directories(${PROJECT_NAME} PUBLIC Lib/headers/)
Aside, this line looks useless and can probably be removed as well:
target_link_directories(${PROJECT_NAME} PRIVATE Lib/headers/)
Link libraries are not usually placed in headers directories.

include_HEADERS and no rule for target needed by 'all-am'

I am trying to compile a library with autotools. The library compiles fine with the following Makefile.am in its source directory library/src
lib_LTLIBRARIES = libgtkchart.la
libgtkchart_la_CFLAGS = $(CFLAGS) -fPIC -Wall $(gtk_CFLAGS)
libgtkchart_la_LIBADD = $(gtk_LIBS) $(glib_LIBS)
libgtkchart_la_LDFLAGS = -fPIC -shared -version-info 1:0:0
libgtkchart_la_SOURCES = gtkchart.c gtkaxis.c gtkbarchart.c gtkchartdata.c gtkchart.h gtkaxis.h gtkbarchart.h gtkbarchartdata.h
but if I add the following line to that file:
include_HEADERS = gtkaxis.h gtkbarchart.h gtkbarchartdata.h gtkchart.h
I get the following error:
make[2]: *** No rule to make target 'gtkbarchartdata.h', needed by 'all-am'. Stop.
My main MAkefile.am in library/ is
SUBDIRS = src
ACLOCAL_AMFLAGS= -I m4
and my configure.ac is
AC_CONFIG_MACRO_DIRS([m4])
AC_INIT([library], [1.0], [me])
AM_PROG_AR
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_DISABLE_STATIC
AC_PROG_LIBTOOL
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
Makefile
src/Makefile
])
LT_INIT
AC_OUTPUT
I can't figure out why the need for a target for that header file.
When you assign header files to the include_HEADERS, automake must
be able to locate the header.
For example, if you have the following directory structure:
src
Makefile.am
GTKBarCharDataDirectory
SomeOtherDirectory
gtkaxis.h
gtkbarchart.h
gtkchart.h
And gtkbarchartdata.h is located in the GTKBarCharDataDirectory directory, then you need to change your include_Headers to be:
include_HEADERS = gtkaxis.h gtkbarchart.h GTKBarCharDataDirectory/gtkbarchartdata.h gtkchart.h
make must know the relative path of the header file, when using the include_Headers variable. This is due to the fact that the HEADER family of variables works differently than the SOURCES family of variables.
automake won't fail if you list a non-existent/not-found header file
in the libgtkchart_la_SOURCES variable, since Header files listed in
a _SOURCES definition will be included in the distribution but
otherwise ignored.
Check here and here for a reference

make file multiple executables

I have a folder called UnitTest. I have several .c files in it. All files contain function 'main'.
Using makefile, I want to compile all .c files of that folder together.
But these files have dependency on other C files which are in different folder.
How can I write make file for this?
eg.
Common\*.c - generate object file
App\*.c - generate object file. - refers to .o files of Common directory
UnitTest\.c - these files should be compiled as executables. Refer *.o from directory App and Common.
Update:
Header files are in seperate directory called \Include
I need a single makefile for this. Please help.
As per the standards every directory will contain one Makefile. So you can have three Makefiles for this job done if you have three directories.
(d) common
|
|---(f) common.h
|---(f) common.c
|---(f) Makefile --- MAkefile for the common folder.
(d) app
|
|---(f) app.h
|---(f) app.c
|---(f) Makefile
(d) unittest
|
|---(f) unittest.h
|---(f) unittest.c
|---(f) Makefile
(f) Makefile --- invoke all makefiles in the mentioned order.
If you want one Makefile to happen all these done, you can do in that way also. Here you have to compile the files by providing paths of the files. order is most impotent.
This is complicated, so we will take it in stages. First, building the object files:
CFLAGS += -I/include
vpath %.h /include
This should be enough to build any object file in Common/, Apps/ or UnitTest/. Test this before going further.
Now to build all of the objects in Common/:
COMMONSOURCES := $(wildcard Common/*.c)
COMMONOBJECTS := $(COMMONSOURCES:.c=.o)
all: $(COMMONOBJECTS)
Test this before going further.
Remove that all rule, and put in a rule for the Common library. (We'll use a static library for now, since it's a little simpler.)
Common/libCommon.a: $(COMMONOBJECTS)
ar -cvq $# $^
Test that much, tell us in the comments how it worked, then we'll build Apps library and the UnitTest executables.

How to link two .c files in this advanced makefile?

I wanna use some functions which are defined in libvmi/driver/xen.c file, in process-list.c file,but I don't know where in Makefile I should link this two ".c" files.
I know how to do this in a simple Makefile but I couldn't find something like that in this Makefile to add linking part of libvmi/driver/xen.c and process-list.c.
This Makefile belongs to a project with several Makefiles.
Thanks for any help!
## Source directory
SUBDIRS =
INCLUDES = -I$(top_srcdir)
AM_LDFLAGS = -L$(top_srcdir)/libvmi/.libs/
LDADD = -lvmi -lm $(LIBS)
c_sources = process-list.c \
libvmi/driver/xen.c
bin_PROGRAMS = module-list process-list map-symbol map-addr dump-memory
module_list_SOURCES = module-list.c
process_list_SOURCES = $(c_sources)
map_symbol_SOURCES = map-symbol.c
map_addr_SOURCES = map-addr.c
dump_memory_SOURCES = dump-memory.c
You shouldn't need to link two .c files, you need to compile them and then link the .o files. If your project makefile is generated, perhaps this happens automatically, if not you would mainly need to add the new .c files to the build.

How to compile a custom folder structure with ndk-build?

I have the next folder structure:
On $My_Eclipse_Project
jni/Android.mk
jni/Application.mk
jni/main.cpp
lib/
On $My_Library_Project
src/
include/
Android.mk
$My_Eclipse_Project/jni/main.cpp is a basic makefile whom call the source to compile, following the instructions from $My_Library_Project/Android.mk
# NOTE:
$(warning Compiling Android.mk from sample_cameraview_activity)
# This path
LOCAL_PATH := $(call my-dir)
$(warning Local path: $(LOCAL_PATH))
# GNU var
include $(CLEAR_VARS)
# Include extra library
include $(mylibrary_INCLUDE)/../Android.mk
# Add openCV
# Add in .bashrc enviroment var
include $(OPENCV_SHARE_MK)/OpenCV.mk
LOCAL_ARM_NEON := true
# Local libraries
LOCAL_LDLIBS += -llog -lGLESv1_CM
# Name library
LOCAL_MODULE := camView
# Local SRC
LOCAL_SRC_FILES := main.cpp
# Shared library
include $(BUILD_SHARED_LIBRARY)
The problem is, $My_Library_Project>Android.mk don't detect the folder structure what is waiting, because my source is on src folder, don't at jni folder. I get:
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
How could i compile the code at $My_Library_Project from $My_Eclipse_Project, and copy the library generated on $My_Eclipse_Project>lib/armeabi, to be used by main.cpp?
Thanks in advance.
2 Options. You can define $NDK_PROJECT_PATH = /your_ndk_path, Makefile auto detect the path and use it to compile, or add a enviroment var with name $NDK_PROJECT_PATH and value /your_ndk_path.

Resources