Skip to content

Fix Compiling with Clang for CMake #1272

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

Closed
wants to merge 1 commit into from

Conversation

JonathanCline
Copy link

I noticed that compiling the library with clang and cmake simply failed due to clang not providing the C++ compile features whatsoever at least with the version of clang I am using. I threw together a quick fix described below:

Added a CMake macro to determine the name of the C++ compiler executable. This is then used to determine if clang (clang++) is being used.

If clang is being used then the target compile features call is skipped.

The justification is that if clang is being used then
using target_compile_features would fail as clang doesn't provide this.

executable. This is then used to determine if clang (clang++) is being used.

If clang is being used then the target compile features call is skipped.

The justification is that if clang is being used then
using `target_compile_features` would fail as clang doesn't provide this.
@JonathanCline
Copy link
Author

Might be a reasonably idea to throw in a check to see if CMAKE_CXX_COMPILE_FEATURES is not empty so that if/when clang has this available it will automatically be used.

@yhirose
Copy link
Owner

yhirose commented May 16, 2022

@sum01, could you review this pull request, since I don't have enough knowledge about CMake. Thanks!

string(REGEX MATCH "[^/\\]*$" _outStr ${CMAKE_CXX_COMPILER})
string(REGEX MATCH "[^\.]+" _outStr ${_outStr})
set(${out_Name} ${_outStr})
endmacro()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Can't you just do

if (NOT CMAKE_CXX_COMPILER MATCHES "clang_regex")
    target_compile_features(...)
endif()

Comment on lines +205 to +217
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
cxx_std_11
cxx_nullptr
cxx_lambdas
cxx_override
cxx_defaulted_functions
cxx_attribute_deprecated
cxx_auto_type
cxx_decltype
cxx_deleted_functions
cxx_range_for
cxx_sizeof_member
)
Copy link
Contributor

@Tachi107 Tachi107 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway I don't see why using target_compile_features() with the low-level compile features is needed; why isn't cxx_std_11 enough? And disabling the call when using Clang would introduce new issues, because CMake wouldn't know that C++11 is required.

In my opinion the low level compile features should be simply dropped. Even CMake's documentation recommends against their use: https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#low-level-individual-compile-features

cc @sum01

@Tachi107
Copy link
Contributor

@JonathanCline what clang version are you using? I can't reproduce your issue.

$ clang++ -v
Debian clang version 13.0.1-3+b2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64

$ CC=clang CXX=clang++ cmake -S . -B build
-- The CXX compiler identification is Clang 13.0.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.W7BzwESP0P/cpp-httplib/build

@sum01
Copy link
Contributor

sum01 commented May 16, 2022

Yeah I wasn't aware it was not recommended. It could instead be edited so only cxx_std_11 is used, as mentioned in the docs.

Regardless, @Tachi107 is right that trying to detect Clang and disable feature settings if it's used isn't a good idea.

Also, I don't have issues using Clang++ on v13.0.1 either, for both header-only and compiled versions of the build. Not sure how @JonathanCline was calling the build function, but using this works just fine on my end.

cd build
# Adding -DHTTPLIB_COMPILE=ON also runs as expected.
cmake -DCMAKE_CXX_COMPILER="clang++" ..
cmake --build .

If needed I can submit a small patch to delete those few unnecessary feature lines, or anyone else can feel free to as well.

Tachi107 added a commit to Tachi107/cpp-httplib that referenced this pull request May 16, 2022
- Enable THREADS_PREFER_PTHREAD_FLAG to use -pthread where supported
- Remove low-level compile features (closes yhirose#1272)
- Remove unneeded DESTINATION options where possible
yhirose pushed a commit that referenced this pull request May 17, 2022
- Enable THREADS_PREFER_PTHREAD_FLAG to use -pthread where supported
- Remove low-level compile features (closes #1272)
- Remove unneeded DESTINATION options where possible
@Tachi107
Copy link
Contributor

@JonathanCline this was closed as I believe that your issues was caused by the low-level compile features. Could you please check if your issue has been actually solved?

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this pull request May 2, 2023
- Enable THREADS_PREFER_PTHREAD_FLAG to use -pthread where supported
- Remove low-level compile features (closes yhirose#1272)
- Remove unneeded DESTINATION options where possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants