cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.13k stars 3.81k forks source link

A compile error occured when I add my own functions in libroach and pkg/ #26414

Closed zwdong1994 closed 6 years ago

zwdong1994 commented 6 years ago

I have wrote a module in the libroach, and using it in the cockroach. As is the following show:

Functions in the libroach:

#ifdef __cplusplus
extern "C" {
#endif

typedef struct result_num_info{
    int result_num;
    int column_num;
    int total_col_num;
}DBres;

typedef struct {
    char* data;
    int len;
    int col_id;
} DBString;

void commit_stmts(char *command);
void get_result_num(DBres* res);
void push_result(DBString *result);

#ifdef __cplusplus
}
#endif

How to call it: Go file's positon: cockroach/pkg/newresult/no_pri_result.go

package newresult

// #cgo CPPFLAGS: -I../../c-deps/libroach/include
// #cgo LDFLAGS: -lstdc++
// #cgo LDFLAGS: -lrocksdb
// #cgo LDFLAGS: -lroach
// #cgo LDFLAGS: -lprotobuf
// #cgo LDFLAGS: -lsnappy
//
// #include <stdlib.h>
// #include <select_from_libroach.h>
import "C"

func return_col_num() (int, int) {
    var res_info C.DBres

    C.get_result_num(&res_info)
    if res_info.column_num > 0 && res_info.result_num >0 {
        return res_info.column_num, res_info.result_num
    } else {
        return 0, 0
    }
}

Besides, I also add the director of this file in the build/common.mk. As is following show:

CGO_PKGS := cli server/status storage/engine ccl/storageccl/engineccl newresult
CGO_UNSUFFIXED_FLAGS_FILES := $(addprefix $(PKG_ROOT)/,$(addsuffix /zcgo_flags.go,$(CGO_PKGS)))
CGO_SUFFIXED_FLAGS_FILES   := $(addprefix $(PKG_ROOT)/,$(addsuffix /zcgo_flags_$(NATIVE_SPECIFIER_TAG).go,$(CGO_PKGS)))
CGO_FLAGS_FILES := $(CGO_UNSUFFIXED_FLAGS_FILES) $(CGO_SUFFIXED_FLAGS_FILES)

$(CGO_UNSUFFIXED_FLAGS_FILES): .ALWAYS_REBUILD

$(CGO_FLAGS_FILES): $(REPO_ROOT)/build/common.mk
    @echo 'GEN $@'
    @echo '// GENERATED FILE DO NOT EDIT' > $@
    @echo >> $@
    @echo '// +build $(if $(findstring $(NATIVE_SPECIFIER_TAG),$@),$(NATIVE_SPECIFIER_TAG),!make)' >> $@
    @echo >> $@
    @echo 'package $(notdir $(@D))' >> $@
    @echo >> $@
    @echo '// #cgo CPPFLAGS: -I$(JEMALLOC_DIR)/include' >> $@
    @echo '// #cgo LDFLAGS: $(addprefix -L,$(PROTOBUF_DIR) $(JEMALLOC_DIR)/lib $(SNAPPY_DIR) $(ROCKSDB_DIR) $(LIBROACH_DIR))' >> $@
    @echo 'import "C"' >> $@

However, there were some compile errors happened. As is following show:

victor@victor:~/workspace/src/github.com/cockroachdb/cockroach$ make build
GOPATH set to /home/victor/workspace
Running make with -j4
GEN pkg/cli/zcgo_flags.go
GEN pkg/server/status/zcgo_flags.go
GEN pkg/storage/engine/zcgo_flags.go
GEN pkg/ccl/storageccl/engineccl/zcgo_flags.go
GEN pkg/newresult/zcgo_flags.go
GEN pkg/sql/zcgo_flags.go
make[1]: Nothing to be done for 'build_lib_static'.
[100%] Built target snappy
[100%] Built target roach
[100%] Built target libprotobuf
[ 88%] Built target roach
[100%] Built target roachccl
[  0%] Built target build_version
[100%] Built target rocksdb
go build -i -o ./cockroach -v  -tags ' make x86_64_linux_gnu' -ldflags ' -X github.com/cockroachdb/cockroach/pkg/build.typ=development -X "github.com/cockroachdb/cockroach/pkg/build.tag=v1.1.5-24-gcc62780-dirty" -X "github.com/cockroachdb/cockroach/pkg/build.utcTime=2018/06/05 09:32:44" -X "github.com/cockroachdb/cockroach/pkg/build.rev=cc627800b064dcc549523b5e61c90c8d7b603ee1" -X "github.com/cockroachdb/cockroach/pkg/build.baseBranch=v1.1.5"' .
github.com/cockroachdb/cockroach/pkg/newresult
github.com/cockroachdb/cockroach/pkg/sql/distsqlrun
# github.com/cockroachdb/cockroach/pkg/sql/distsqlrun
/usr/bin/ld: cannot find -ljemalloc
collect2: error: ld returned 1 exit status
# github.com/cockroachdb/cockroach/pkg/newresult
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0xba): undefined reference to `rocksdb::Cleanable::~Cleanable()'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0xf4): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long)'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsEPNS_18ColumnFamilyHandleERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x111): undefined reference to `operator delete[](void*)'
/home/victor/workspace/native/x86_64-linux-gnu/libroach/libroach.a(rocksIO_op.cpp.o): In function `rocksdb::DB::Get(rocksdb::ReadOptions const&, rocksdb::Slice const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x6d): undefined reference to `rocksdb::Cleanable::Cleanable()'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0xcd): undefined reference to `operator delete(void*)'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0xd7): undefined reference to `rocksdb::Cleanable::~Cleanable()'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x11c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long)'
rocksIO_op.cpp:(.text._ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN7rocksdb2DB3GetERKNS_11ReadOptionsERKNS_5SliceEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x156): undefined reference to `operator delete[](void*)'
/home/victor/workspace/native/x86_64-linux-gnu/libroach/libroach.a(rocksIO_op.cpp.o): In function `_GLOBAL__sub_I__ZN7rocksIOC2Ev':
rocksIO_op.cpp:(.text.startup+0xa): undefined reference to `std::ios_base::Init::Init()'
rocksIO_op.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()'
/home/victor/workspace/native/x86_64-linux-gnu/libroach/libroach.a(rocksIO_op.cpp.o):(.eh_frame+0x113): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Makefile:177: recipe for target 'cockroach' failed
make: *** [cockroach] Error 2

According to the error output, it seemly like I have lost -lstdc++. However, I have added it before the compile. So, what should I do to solve this problem?

zwdong1994 commented 6 years ago

I find out the reason that cause the problem. The sequence of the declaration will cause this problem. For example, at this instance, if I change the sequence of the declaraton in the no_pri_result.go, the error will disapear.

// #cgo CPPFLAGS: -I../../c-deps/libroach/include
// #cgo LDFLAGS: -lroach
// #cgo LDFLAGS: -lrocksdb
// #cgo LDFLAGS: -lprotobuf
// #cgo LDFLAGS: -lsnappy
// #cgo LDFLAGS: -lstdc++
//
// #include <stdlib.h>
// #include <select_from_libroach.h>

In fact, I'm not sure whether this feature is right. However, after the modification of the declaration sequence, the problems were solved.