boostorg / json

A C++11 library for parsing and serializing JSON to and from a DOM container in memory.
https://boost.org/libs/json
Boost Software License 1.0
434 stars 96 forks source link

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

Open DBizz22 opened 1 month ago

DBizz22 commented 1 month ago

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.

grisumbras commented 1 month ago

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?

DBizz22 commented 1 month ago

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

grisumbras commented 1 month ago

Can you please post the contents of that file?

DBizz22 commented 1 month ago

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

grisumbras commented 1 month ago

Is there really nothing in the line after #include?

DBizz22 commented 1 month ago

include "cstddef"

grisumbras commented 1 month ago

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.

DBizz22 commented 1 month ago

[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

grisumbras commented 1 month ago

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; }
DBizz22 commented 1 month ago

[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

grisumbras commented 1 month ago

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?

pdimov commented 1 month ago

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

DBizz22 commented 1 month ago

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)

grisumbras commented 1 month ago

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

DBizz22 commented 1 month ago

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

Please, do you have any suggestions?

pdimov commented 1 month ago

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

DBizz22 commented 1 month ago

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

grisumbras commented 1 month ago

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?

DBizz22 commented 1 month ago

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

grisumbras commented 1 month ago

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