json icon indicating copy to clipboard operation
json copied to clipboard

Multiple Errors when linking a library built with boost.json to a unit test executable

Open DBizz22 opened this issue 1 year ago • 20 comments

PLEASE DON'T FORGET TO "STAR" THIS REPOSITORY :)

Version of Boost

Version 1.85.0-4 from the msys2 mingw-w64-boost package boost1 boost2

Steps necessary to reproduce the problem

CMakeLists.txt for build the unit test add_executable(unitTest UnitTest.cpp) target_link_libraries(unitTest PRIVATE httpClientMock CppUTest::CppUTest CppUTest::CppUTestExt) cpputest_discover_tests(unitTest)

All relevant compiler information

g++ (Rev1, Built by MSYS2 project) 14.2.0

I've tried building both the header-only build and builds dependent on the static boost_json library but they both produce the same similar errors.

DBizz22 avatar Sep 17 '24 12:09 DBizz22

This looks like a problem with Boost.Container. If my guessing is correct, some Container header is missing an include. Can you please include boost/container/detail/placement_new.hpp instead of boost/json/src.hpp? Also, in the future could you please post copied text and not screenshots?

grisumbras avatar Sep 17 '24 12:09 grisumbras

ok, Here is the result [main] Building folder: C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config [build] Starting build [proc] Executing command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- [build] [ 50%] Built target CppUTest [build] [100%] Built target CppUTestExt [build] [100%] Built target httpClientInterface [build] [100%] Built target coreApiInterface [build] [100%] Building CXX object bin/lib/api/alphavantageApi/CMakeFiles/alphavantageStatic.dir/alphavantage.cpp.obj [build] [100%] Linking CXX static library libalphavantageStatic.a [build] [100%] Built target alphavantageStatic [build] [100%] Built target httpClientMock [build] [100%] Building CXX object test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/forexApiUnitTest.cpp.obj [build] In file included from C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/src/lib/api/alphavantageApi/include/alphavantage.hpp:6, [build] from C:\Users\BIZZ\Documents\Vscode\Projects\Stock_Market_Simulator\test\alphavantageApiSuiteTest\forexApiUnitTest.cpp:7: [build] C:/msys64/ucrt64/include/boost/container/detail/placement_new.hpp:18:14: error: declaration of 'operator new' as non-function [build] 18 | inline void *operator new(std::size_t, void *p, boost_container_new_t) [build] | ^~~~~~~~ [build] mingw32-make[3]: *** [test\alphavantageApiSuiteTest\CMakeFiles\alphavantageApiTest.dir\build.make:76: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/forexApiUnitTest.cpp.obj] Error 1 [build] mingw32-make[2]: *** [CMakeFiles\Makefile2:10538: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/all] Error 2 [build] mingw32-make[1]: *** [CMakeFiles\Makefile2:10545: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/rule] Error 2 [build] mingw32-make: *** [Makefile:4726: alphavantageApiTest] Error 2 [proc] The command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- exited with code: 2 [driver] Build completed: 00:00:05.138 [build] Build finished with exit code 2

DBizz22 avatar Sep 17 '24 12:09 DBizz22

Can you please post the contents of that file?

grisumbras avatar Sep 17 '24 12:09 grisumbras

#ifndef BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP #define BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP /////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/container for documentation. // ///////////////////////////////////////////////////////////////////////////////

#include

struct boost_container_new_t{};

//avoid including inline void *operator new(std::size_t, void *p, boost_container_new_t) { return p; }

inline void operator delete(void *, void *, boost_container_new_t) {}

#endif //BOOST_CONTAINER_DETAIL_PLACEMENT_NEW_HPP

DBizz22 avatar Sep 17 '24 12:09 DBizz22

Is there really nothing in the line after #include?

grisumbras avatar Sep 17 '24 12:09 grisumbras

#include "cstddef"

DBizz22 avatar Sep 17 '24 12:09 DBizz22

Ok, now what happens if you don't include the Container header, and instead put the following?

#include <cstddef>
std::size_t n;

BTW, if you want to put a bunch of code in a GH comment, use triple backticks (```) before and after it.

grisumbras avatar Sep 17 '24 13:09 grisumbras

[main] Building folder: C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config [build] Starting build [proc] Executing command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- [build] [ 50%] Built target CppUTest [build] [100%] Built target CppUTestExt [build] [100%] Built target httpClientInterface [build] [100%] Built target coreApiInterface [build] [100%] Building CXX object bin/lib/api/alphavantageApi/CMakeFiles/alphavantageStatic.dir/alphavantage.cpp.obj [build] [100%] Linking CXX static library libalphavantageStatic.a [build] [100%] Built target alphavantageStatic [build] [100%] Built target httpClientMock [build] [100%] Building CXX object test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/forexApiUnitTest.cpp.obj [build] [100%] Linking CXX executable alphavantageApiTest.exe [build] C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../bin/lib/api/alphavantageApi/libalphavantageStatic.a(alphavantage.cpp.obj):C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/src/lib/api/alphavantageApi/include/alphavantage.hpp:7: multiple definition of `n'; CMakeFiles\alphavantageApiTest.dir/objects.a(forexApiUnitTest.cpp.obj):C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/src/lib/api/alphavantageApi/include/alphavantage.hpp:7: first defined here [build] collect2.exe: error: ld returned 1 exit status [build] mingw32-make[3]: *** [test\alphavantageApiSuiteTest\CMakeFiles\alphavantageApiTest.dir\build.make:104: test/alphavantageApiSuiteTest/alphavantageApiTest.exe] Error 1 [build] mingw32-make[2]: *** [CMakeFiles\Makefile2:10538: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/all] Error 2 [build] mingw32-make[1]: *** [CMakeFiles\Makefile2:10545: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/rule] Error 2 [build] mingw32-make: *** [Makefile:4726: alphavantageApiTest] Error 2 [proc] The command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- exited with code: 2 [driver] Build completed: 00:00:06.131 [build] Build finished with exit code 2

It builds successfully after hiding std::size_t n and the boost.json functions i used

DBizz22 avatar Sep 17 '24 13:09 DBizz22

Now, please try

#include <cstddef>

struct boost_container_new_t{};

inline void *operator new(std::size_t, void *p, boost_container_new_t)
{ return p; }

grisumbras avatar Sep 17 '24 13:09 grisumbras

[main] Building folder: C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config [build] Starting build [proc] Executing command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- [build] [ 0%] Built target httpClientInterface [build] [ 0%] Built target httpClientMock [build] [ 0%] Built target coreApiInterface [build] [ 33%] Built target boost_container [build] [ 33%] Built target boost_json [build] [ 33%] Building CXX object bin/lib/api/alphavantageApi/CMakeFiles/alphavantageStatic.dir/alphavantage.cpp.obj [build] [ 33%] Linking CXX static library libalphavantageStatic.a [build] [ 33%] Built target alphavantageStatic [build] [ 66%] Built target CppUTest [build] [100%] Built target CppUTestExt [build] [100%] Building CXX object test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/forexApiUnitTest.cpp.obj [build] In file included from C:\Users\BIZZ\Documents\Vscode\Projects\Stock_Market_Simulator\test\alphavantageApiSuiteTest\forexApiUnitTest.cpp:7: [build] C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/src/lib/api/alphavantageApi/include/alphavantage.hpp:12:14: error: declaration of 'operator new' as non-function [build] 12 | inline void *operator new(std::size_t, void *p, boost_container_new_t) [build] | ^~~~~~~~ [build] mingw32-make[3]: *** [test\alphavantageApiSuiteTest\CMakeFiles\alphavantageApiTest.dir\build.make:76: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/forexApiUnitTest.cpp.obj] Error 1 [build] mingw32-make[2]: *** [CMakeFiles\Makefile2:10542: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/all] Error 2 [build] mingw32-make[1]: *** [CMakeFiles\Makefile2:10549: test/alphavantageApiSuiteTest/CMakeFiles/alphavantageApiTest.dir/rule] Error 2 [build] mingw32-make: *** [Makefile:4726: alphavantageApiTest] Error 2 [proc] The command: C:\msys64\ucrt64\bin\cmake.EXE --build "C:/Users/BIZZ/Documents/Vscode/Projects/Stock_Market_Simulator/build/MinGW Debug Config" --target alphavantageApiTest -- exited with code: 2 [driver] Build completed: 00:00:05.514 [build] Build finished with exit code 2

DBizz22 avatar Sep 17 '24 13:09 DBizz22

I am at a loss. Are you sure

#include <cstddef>
std::size_t n = 1;

compiles?

The only reason for your error I can imagine is that <cstddef> for some reason doesn't have a definition for std::size_t.

BTW, what compiler flags are you using to build?

grisumbras avatar Sep 17 '24 13:09 grisumbras

The problem is the macro definition at line 83 of CppUTest/MemoryLeakDetectorMacros.h as visible on the screenshot above.

pdimov avatar Sep 17 '24 14:09 pdimov

I am at a loss. Are you sure

#include <cstddef>
std::size_t n = 1;

compiles?

The only reason for your error I can imagine is that <cstddef> for some reason doesn't have a definition for std::size_t.

BTW, what compiler flags are you using to build?

It compiles but with std::size_t commented out. only compiler flag effect : set(CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON)

DBizz22 avatar Sep 17 '24 14:09 DBizz22

What @pdimov said. CppUTest defines a macro that messes up operator new declarations.

grisumbras avatar Sep 17 '24 14:09 grisumbras

What @pdimov said. CppUTest defines a macro that messes up operator new declarations.

Please, do you have any suggestions?

DBizz22 avatar Sep 17 '24 14:09 DBizz22

You need to include the CppUTest headers last, after the Boost.JSON headers (and probably after everything else to avoid similar issues elsewhere.)

pdimov avatar Sep 17 '24 15:09 pdimov

You need to include the CppUTest headers last, after the Boost.JSON headers (and probably after everything else to avoid similar issues elsewhere.)

Unfortunately, that does not seem to fix the issue

DBizz22 avatar Sep 18 '24 00:09 DBizz22

Well, it should fix the issue of errors in Boost headers. The macro definition can't affect declarations before it. What's the error you're getting now?

grisumbras avatar Sep 18 '24 09:09 grisumbras

includes in the UnitTest.cpp

#include <iostream>
#include <string>
#include "httpClientMock.cpp"
#include "alphavantage.hpp"
#include <CppUTest/TestHarness.h>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTestExt/MockSupport.h>

boost/json/src.hpp is declared in the alphavantage.hpp

The errors are still the same

DBizz22 avatar Sep 18 '24 13:09 DBizz22

Does #include "httpClientMock.cpp" include any of the CppUTest headers?

grisumbras avatar Oct 09 '24 13:10 grisumbras

Closing as there was no response.

grisumbras avatar Dec 14 '24 11:12 grisumbras