As written above, I try to compile one native library to use it within JNI. But I get the following error:
/src/main/cpp/app1.cpp.o: multiple definition of '...'
/src/main/cpp/mylib.cpp.o: previous definition here
This only appears when I use the .hh data, which "include full template code". The stuff that is multiple defined is in a precompiled library, with the headers included through 'HEADER_FILES'.
Here is my CMakeLists.txt:
set(HEARDER_SRC_DIRECTORY ${CMAKE_SOURCE_DIR}/../../header/src)
include_directories(${HEADER_SRC_DIRECTORY})
set(HEADER_FILES ${HEADER_SRC_DIRECTORY}/header1.h ${HEADER_SRC_DIRECTORY}/header2.h ${HEADER_SRC_DIRECTORY}/header1.hh ${HEADER_SRC_DIRECTORY}/header2.hh)
add_library(
mylib
SHARED
src/main/mylib.cpp
${HEADER_FILES}
src/main/app1.h
src/main/app1.cpp
)
find_library(
log-lib
log
)
target_link_libraries(
mylib
${CMAKE_SOURCE_DIR}/libs/liby.a # precompiled lib
${CMAKE_SOURCE_DIR}/libs/libz.a # precompiled lib
${log-lib}
)
How can I fix that? As I understand it each .cpp gets compiled on its own before they get linked and then they already have the same definitions. Of course, they have, that is why I link app1 in mylib, so I can access some functions. Where is my logical error? Will splitting them up help?
What mylib.cpp is including:
#include <jni.h>
#include <string>
#include "app1.h"
What app1.h is including:
#include "jni.h"
#include "header1.h"
#include "header1.hh"
#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
Here is one part of the definitions that is double (inside the header1.h):
namespace nsp {
Class::Class(Able& able, std::string name)
: _able(able), _iBlock(0), _size(0), _name(name)
{ }
void Serializer::resetCounter()
{
_iBlock = 0;
}
....
}