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

strings.h should be renamed to avoid standard library conflicts. #153

Open
colugomusic opened this issue Oct 4, 2024 · 5 comments · May be fixed by #154
Open

strings.h should be renamed to avoid standard library conflicts. #153

colugomusic opened this issue Oct 4, 2024 · 5 comments · May be fixed by #154
Assignees
Labels
enhancement New feature or request

Comments

@colugomusic
Copy link

This file can cause build problems because it shares its name with a standard header, for example in my project:

In file included from /home/chris/dv/env/prefix/include/boost/fusion/iterator/deref.hpp:11,
                 from /home/chris/dv/env/prefix/include/boost/process/v1/async.hpp:41,
                 from /home/chris/dv/env/prefix/include/boost/process/v1.hpp:9,
                 from /home/chris/dv/env/prefix/include/boost/process.hpp:27,
                 from /home/chris/dv/cpm_cache/scuff/a4159ed6e8606db8a050dca7dafd4360317f0ddb/common/include/common/os.hpp:82,
                 from /home/chris/dv/cpm_cache/scuff/a4159ed6e8606db8a050dca7dafd4360317f0ddb/test-host/src/main.cpp:1:
/home/chris/dv/env/prefix/include/boost/fusion/support/iterator_base.hpp:22:14: error: macro "cast" requires 2 arguments, but only 1 given
   22 |         cast() const BOOST_NOEXCEPT
      |              ^
In file included from /home/chris/dv/cpm_cache/nappgui/b51a6f10f9b7255fd2367215f1947aa7747ab7e4/src/sewer/sewer.hxx:17,
                 from /home/chris/dv/cpm_cache/nappgui/b51a6f10f9b7255fd2367215f1947aa7747ab7e4/src/osbs/osbs.hxx:17,
                 from /home/chris/dv/cpm_cache/nappgui/b51a6f10f9b7255fd2367215f1947aa7747ab7e4/src/core/core.hxx:17,
                 from /home/chris/dv/cpm_cache/nappgui/b51a6f10f9b7255fd2367215f1947aa7747ab7e4/src/core/strings.h:13,
                 from /usr/include/string.h:462,
                 from /usr/include/c++/13/cstring:42,
                 from /home/chris/dv/env/prefix/include/boost/assert/source_location.hpp:15,
                 from /home/chris/dv/env/prefix/include/boost/exception/exception.hpp:9,
                 from /home/chris/dv/env/prefix/include/boost/throw_exception.hpp:21,
                 from /home/chris/dv/env/prefix/include/boost/process/v1/detail/config.hpp:34,
                 from /home/chris/dv/env/prefix/include/boost/process/v1/detail/basic_cmd.hpp:10,
                 from /home/chris/dv/env/prefix/include/boost/process/v1/args.hpp:33,
                 from /home/chris/dv/env/prefix/include/boost/process/v1.hpp:8:
/home/chris/dv/cpm_cache/nappgui/b51a6f10f9b7255fd2367215f1947aa7747ab7e4/src/sewer/config.hxx:156: note: macro "cast" defined here
  156 | #define cast(ptr, type) ((type*)(ptr))

As you can see, because /nappgui/.../src/core/ is included as a header search path, for some reason GCC 13 tries to use nappgui's strings.h first, instead of the system strings.h. I'm not sure yet if there is an easy workaround for this.

It may also be wise to add a namespace prefix for macros such as cast defined in config.hxx, for example nappgui_cast, to avoid the potential conflict also shown above.

@frang75 frang75 self-assigned this Oct 5, 2024
@frang75 frang75 added the enhancement New feature or request label Oct 5, 2024
@frang75
Copy link
Owner

frang75 commented Oct 5, 2024

At the moment, you can disable the NAppGUI cast() macro, before include boost.

#if defined(cast)
#undef cast
#endif
#include "boost.h"

@colugomusic
Copy link
Author

colugomusic commented Oct 5, 2024

At the moment, you can disable the NAppGUI cast() macro, before include boost.

#if defined(cast)
#undef cast
#endif
#include "boost.h"

This would not make any difference, because the issue is not cause by the order of includes. It is caused by the mere existence of nappgui's "strings.h" in the compiler's list of paths to search for header files.

If you look at the include list I posted above, you will see that nappgui's strings.h is being included by the system's /usr/include/string.h. There is nothing that can be done in code to fix that. Your #undef cast would have no effect because when the boost header is included on the next line, nappgui's strings.h would then be incorrectly included and cast would be redefined.

To make matters worse I don't think it is even possible to force cmake to add `-I/usr/include" to the compiler flags before everything else, to ensure that the system headers are found first.

"strings.h" is a standard POSIX header file name so I think it should be renamed to avoid conflicts like this, since there doesn't seem to be any workaround (at least in a cmake build).

@colugomusic colugomusic linked a pull request Oct 5, 2024 that will close this issue
@akacastor
Copy link

I have also run into a problem related to this issue. I am trying to build a NAppGUI application that links with the OpenCV library. I am able to build successfully in macOS and Windows, but the Linux build fails due to conflicts in the OpenCV and NAppGUI header files.

Because OpenCV has a C++ interface, I created a .cpp file to act as the interface between the NAppGUI project and OpenCV. By not including the NAppGUI header files in that .cpp file, it is able to compile on macOS and Windows systems. However, the Linux build system fails. (maybe due to different order of searching include directories?)

(I tried undefining the cast macro but as mentioned above it made no difference.)

Errors during compilation in Linux (Ubuntu 24.04.1):

[ 33%] Building CXX object src/CMakeFiles/testcv.dir/use_opencv.cpp.o
In file included from /usr/local/include/opencv4/opencv2/calib3d.hpp:50,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:56,
                 from /home/chris/proj/testcv/src/use_opencv.cpp:5:
/usr/local/include/opencv4/opencv2/core/affine.hpp:271:47: error: macro "cast" requires 2 arguments, but only 1 given
  271 |         template <typename Y> Affine3<Y> cast() const;
      |                                               ^
In file included from /usr/local/nappgui/inc/sewer/sewer.hxx:17,
                 from /usr/local/nappgui/inc/osbs/osbs.hxx:17,
                 from /usr/local/nappgui/inc/core/core.hxx:17,
                 from /usr/local/nappgui/inc/core/strings.h:13,
                 from /usr/include/string.h:462,
                 from /usr/include/c++/13/cstring:42,
                 from /usr/local/include/opencv4/opencv2/core/cvstd.hpp:53,
                 from /usr/local/include/opencv4/opencv2/core/base.hpp:58,
                 from /usr/local/include/opencv4/opencv2/core.hpp:53,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:52:
/usr/local/nappgui/inc/sewer/config.hxx:180: note: macro "cast" defined here
  180 | #define cast(ptr, type) ((type*)(ptr))
      | 
/usr/local/include/opencv4/opencv2/core/affine.hpp:595:37: error: macro "cast" requires 2 arguments, but only 1 given
  595 | cv::Affine3<Y> cv::Affine3<T>::cast() const
      |                                     ^
/usr/local/nappgui/inc/sewer/config.hxx:180: note: macro "cast" defined here
  180 | #define cast(ptr, type) ((type*)(ptr))
      | 

Test was done using a file use_opencv.cpp:

#include <opencv2/opencv.hpp>

void test_func(void)
{
    return;
}

The CMakeLists.txt file in the project source directory:

nap_desktop_app(testcv "" NRC_NONE)

# add OpenCV to the build
find_package(OpenCV REQUIRED)

# add include dir for OpenCV header files
include_directories( ${OpenCV_INCLUDE_DIRS} )

# add the OpenCV libraries for linker
target_link_libraries(testcv ${OpenCV_LIBS})

# C++17 standard covers OpenCV 4
nap_target_cxx_standard(testcv 17)

# C11 standard covers OpenCV 4
nap_target_c_standard(testcv 11)

IF(!WIN32)
# disable c11-extensions warning
target_compile_options(testcv PRIVATE -Wno-c11-extensions)
ENDIF()

@frang75
Copy link
Owner

frang75 commented Dec 14, 2024

Yes, its planned to resolved this issue for NAppGUI 1.5. strings.h is a very common header and affect a lot of files.

@frang75
Copy link
Owner

frang75 commented Dec 20, 2024

Fixed in this commit: 07b4812

The problem is not with renaming strings.h. Rather, the problem was with the build system, which included internal library directories in the global search path. Now it is necessary to prepend the library name when including strings.h (or another NAppGUI file).

#include <core/strings.h>  // To include NAppGUI strings.
#include <strings.h>          // To include stdlib strings.

All changes are handled by CMake and <nappgui.h> header, nothing special needs to be done. It is recommended to remove the /build directory after updating the repo. The only change to be noted in applications is in include osmain() macro:

// Before
#include "osmain.h"
osmain(i_create, i_destroy, "", App)

// Now
#include <osapp/osmain.h>
osmain(i_create, i_destroy, "", App)

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

Successfully merging a pull request may close this issue.

3 participants