Skip to content

Commit 79cfb80

Browse files
committed
Add inline declaration to avoid ODR violation. A test is also included.
1 parent ce847d5 commit 79cfb80

File tree

10 files changed

+927
-923
lines changed

10 files changed

+927
-923
lines changed

examples/tensor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ find_library(TENSORFLOW_LIB tensorflow HINT $ENV{HOME}/libtensorflow2/lib)
55

66
set(CMAKE_CXX_STANDARD 17)
77

8-
add_executable(example main.cpp)
8+
add_executable(example main.cpp odr.cpp)
99
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include)
1010
target_link_libraries(example "${TENSORFLOW_LIB}")

examples/tensor/odr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "cppflow/cppflow.h"
2+
3+
// Do NOT remove this file
4+
// test ODR violation

include/cppflow/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace cppflow {
1616

17-
bool status_check(TF_Status* status) {
17+
inline bool status_check(TF_Status* status) {
1818
if (TF_GetCode(status) != TF_OK) {
1919
throw std::runtime_error(TF_Message(status));
2020
}

include/cppflow/cppflow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace cppflow {
2828
******************************/
2929

3030
namespace cppflow {
31-
std::string version() {
31+
inline std::string version() {
3232
return "TensorFlow: " + std::string(TF_Version()) + " CppFlow: 2.0.0";
3333
}
3434
}

include/cppflow/datatype.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace cppflow {
1313
* @return A string representing dt
1414
*
1515
*/
16-
std::string to_string(datatype dt) {
16+
inline std::string to_string(datatype dt) {
1717
switch (dt) {
1818
case TF_FLOAT:
1919
return "TF_FLOAT";
@@ -103,7 +103,7 @@ namespace cppflow {
103103
* @return The stream os after inserting the string representation of dt
104104
*
105105
*/
106-
std::ostream& operator<<(std::ostream& os, datatype dt) {
106+
inline std::ostream& operator<<(std::ostream& os, datatype dt) {
107107
os << to_string(dt);
108108
return os;
109109
}

include/cppflow/model.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace cppflow {
4242

4343
namespace cppflow {
4444

45-
model::model(const std::string &filename) {
45+
inline model::model(const std::string &filename) {
4646
this->graph = {TF_NewGraph(), TF_DeleteGraph};
4747

4848
// Create the session.
@@ -64,7 +64,7 @@ namespace cppflow {
6464
status_check(context::get_status());
6565
}
6666

67-
std::vector<std::string> model::get_operations() const {
67+
inline std::vector<std::string> model::get_operations() const {
6868
std::vector<std::string> result;
6969
size_t pos = 0;
7070
TF_Operation* oper;
@@ -76,12 +76,12 @@ namespace cppflow {
7676
return result;
7777
}
7878

79-
std::tuple<std::string, int> parse_name(const std::string& name) {
79+
inline std::tuple<std::string, int> parse_name(const std::string& name) {
8080
auto idx = name.find(':');
8181
return (idx == -1 ? std::make_tuple(name, 0) : std::make_tuple(name.substr(0, idx), std::stoi(name.substr(idx + 1))));
8282
}
8383

84-
std::vector<tensor> model::operator()(std::vector<std::tuple<std::string, tensor>> inputs, std::vector<std::string> outputs) {
84+
inline std::vector<tensor> model::operator()(std::vector<std::tuple<std::string, tensor>> inputs, std::vector<std::string> outputs) {
8585

8686
std::vector<TF_Output> inp_ops(inputs.size());
8787
std::vector<TF_Tensor*> inp_val(inputs.size(), nullptr);
@@ -128,7 +128,7 @@ namespace cppflow {
128128
return result;
129129
}
130130

131-
tensor model::operator()(const tensor& input) {
131+
inline tensor model::operator()(const tensor& input) {
132132
return (*this)({{"serving_default_input_1", input}}, {"StatefulPartitionedCall"})[0];
133133
}
134134
}

include/cppflow/ops.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,29 @@ namespace cppflow {
5656

5757
// Operators
5858

59-
tensor operator+(const tensor& x, const tensor& y) {
59+
inline tensor operator+(const tensor& x, const tensor& y) {
6060
return add(x, y);
6161
}
6262

63-
tensor operator-(const tensor& x, const tensor& y) {
63+
inline tensor operator-(const tensor& x, const tensor& y) {
6464
return sub(x, y);
6565
}
6666

67-
tensor operator*(const tensor& x, const tensor& y) {
67+
inline tensor operator*(const tensor& x, const tensor& y) {
6868
return mul(x, y);
6969
}
7070

71-
tensor operator/(const tensor& x, const tensor& y) {
71+
inline tensor operator/(const tensor& x, const tensor& y) {
7272
return div(x, y);
7373
}
7474

75-
std::ostream& operator<<(std::ostream& os, const cppflow::tensor& t) {
75+
inline std::ostream& operator<<(std::ostream& os, const cppflow::tensor& t) {
7676
std::string res = to_string(t);
7777
return os << res;
7878
}
7979

8080

81-
std::string to_string(const tensor &t) {
81+
inline std::string to_string(const tensor &t) {
8282
auto res_tensor = string_format({t.shape(), t}, "(tensor: shape=%s, data=\n%s)");
8383
auto res_tensor_h = res_tensor.get_tensor();
8484

include/cppflow/ops_generator/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def code(self):
142142
# C++ function body
143143
template = textwrap.dedent('''
144144
{}
145-
{} {}({}{}) {{
145+
inline {} {}({}{}) {{
146146
147147
// Define Op
148148
std::unique_ptr<TFE_Op, decltype(&TFE_DeleteOp)> op(TFE_NewOp(context::get_context(), "{}", context::get_status()), &TFE_DeleteOp);

0 commit comments

Comments
 (0)