Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The header-only library seems broken #115

Open
1flei opened this issue Sep 17, 2019 · 1 comment
Open

The header-only library seems broken #115

1flei opened this issue Sep 17, 2019 · 1 comment

Comments

@1flei
Copy link

1flei commented Sep 17, 2019

When I try to include "falconn/lsh_nn_table.h" from multiple cpp files, it will cause "multiple definition of ***" linking error for a lot of fht functions, whereas "normal" header-only library should not have this problem (e.g. Eigen).

A minimum example that triggers this problem may be like:
a.cpp
#include "falconn/lsh_nn_table.h"

b.cpp
#include "falconn/lsh_nn_table.h"

main.cpp
#include
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}

CMakeLists.txt
PROJECT(TEST_FALCONN)

set (CMAKE_CXX_STANDARD 17)

include_directories("FALCONN/src/include")
include_directories("FALCONN/external/simple-serializer")
include_directories("FALCONN/external/eigen")

ADD_EXECUTABLE(test ${DIR_SRCS}
"a.cpp"
"b.cpp"
"main.cpp"
)

and when linking a.o and b.o together, it will report multiple definition of *** (e.g. helper_float_/helper_double_/...).

I tried cmake 3.12.0 and gcc/g++6.5/8.0 although I do not think it is the problem of the version of compiler/toolchain.

@OancaAndrei
Copy link

I was facing the same issue. After some digging, I've patched it by moving the last include line from lsh_nn_table.h:

#include "wrapper/cpp_wrapper_impl.h"

to a new file lsh_nn_table_impl.h:

#ifndef __WRAPPER_IMPL_H__
#define __WRAPPER_IMPL_H__

#include "lsh_nn_table.h"
#include "wrapper/cpp_wrapper_impl.h"

#endif

The trick is to include only once per "project" the lsh_nn_table_impl.h file, ideally in one of your cpp files (so the definitions lay in only one object file of your project) and then use lsh_nn_table.h where needed across your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants