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

Cannot compile example_trivial.cpp in Linux #2

Closed
hungptit opened this issue Mar 14, 2020 · 9 comments
Closed

Cannot compile example_trivial.cpp in Linux #2

hungptit opened this issue Mar 14, 2020 · 9 comments
Labels
question Further information is requested

Comments

@hungptit
Copy link

I try to compile example_trivial.cpp in Linux using clang++-9.0.1 and get below compilation errors. Do you have any suggestion?

Thanks,
Hung

[ 50%] Building CXX object CMakeFiles/test_quill.dir/test_quill.cpp.o                                                                                                                                                                 
In file included from /home/hungptit/working/cppidioms/src/test_quill.cpp:1:                                                                                                                                                          
In file included from /home/hungptit/working/cppidioms/src/../3p/include/quill/Quill.h:8:                                                                                                                                             
/home/hungptit/working/cppidioms/src/../3p/include/quill/detail/LogMacros.h:39:62: error: token pasting of ',' and                                                                                                                    
      __VA_ARGS__ is a GNU extension [-Werror,-Wgnu-zero-variadic-macro-arguments]                                                                                                                                                    
    QUILL_LOGGER_CALL(logger, quill::LogLevel::TraceL3, fmt, ##__VA_ARGS__)                                                                                                                                                           
                                                             ^                                                                                                                                                                        
/home/hungptit/working/cppidioms/src/../3p/include/quill/detail/LogMacros.h:33:54: error: token pasting of ',' and                                                                                                                    
      __VA_ARGS__ is a GNU extension [-Werror,-Wgnu-zero-variadic-macro-arguments]                                                                                                                                                    
    logger->log<log_statement_level>(&log_line_info, ##__VA_ARGS__)...                                                                                                                                                                
                                                     ^                                                                                                                                                                                
/home/hungptit/working/cppidioms/src/../3p/include/quill/detail/LogMacros.h:46:62: error: token pasting of ',' and                                                                                                                    
      __VA_ARGS__ is a GNU extension [-Werror,-Wgnu-zero-variadic-macro-arguments]                                                                                                                                                    
    QUILL_LOGGER_CALL(logger, quill::LogLevel::TraceL2, fmt, ##__VA_ARGS__)                                                                                                                                                           
                                                             ^                                                                                                                                                                        
/home/hungptit/working/cppidioms/src/../3p/include/quill/detail/LogMacros.h:33:54: error: token pasting of ',' and                                                                                                                    
      __VA_ARGS__ is a GNU extension [-Werror,-Wgnu-zero-variadic-macro-arguments]                                                                                                                                                    
    logger->log<log_statement_level>(&log_line_info, ##__VA_ARGS__)...                                                              

And this is the compilation command

{                                                                                                                                                                                                                                     
  "directory": "/home/hungptit/working/cppidioms/src",                                                                                                                                                                                
  "command": "clang++   -I/home/hungptit/working/cppidioms/src/../3p/include -I/home/hungptit/working/cppidioms/src/../src    -O3  -march=native  -std=c++17  -Wall  -pedantic  -pedantic-errors  -fstrict-aliasing  -Wstrict-aliasing  -DFMT_HEADER_ONLY  -DCEREAL_RAPIDJSON_HAS_CXX11_RVALUE_REFS  -flto  -static -O3 -DNDEBUG   -o CMakeFiles/test_quill.dir/test_quill.cpp.o -c /home/hungptit/working/cppidioms/src/test_quill.cpp",                                  
  "file": "/home/hungptit/working/cppidioms/src/test_quill.cpp"                                                                                                                                                                       
},
@odygrd
Copy link
Owner

odygrd commented Mar 22, 2020

Hello, thanks for reporting.

I am unable to reproduce on Ubuntu using clang++9,0 and the following compilation flags :

-O3 -march=native -std=c++17 -Wall -pedantic -pedantic-errors -fstrict-aliasing -Wstrict-aliasing -static -DNDEBUG -std=gnu++17

clang version 9.0.0-2 (tags/RELEASE_900/final)

It looks related to this hamon-in/cpslib#30

Could you please :

  1. Double check you are using clang++9.1. Please do run clang++ --version on a terminal
  2. In your compilation command above use clang++-9.1 instead of clang++
  3. Remove -pedantic -pedantic-errors from your compilation command if step 2 does not work and try again

@odygrd odygrd added the question Further information is requested label Mar 23, 2020
@hungptit
Copy link
Author

Hi,

I can compile above example after removing -pedantic and -pedantic-errors flags, however, the compiled binary give me a segv. You can find my build configuration here https://github.com/hungptit/cppidioms/blob/test_quill/src/test_quill.cpp. I take a quick look at the issue using gdb and the error seem to be related to thread

(gdb) run
Starting program: /home/hungptit/working/cppidioms/src/test_quill 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff7ffa700 (LWP 5916)]

Thread 1 "test_quill" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
#1  0x00000000004e0e33 in std::thread::join() ()
(gdb) up
#2  0x0000000000403e79 in quill::detail::BackendWorker::stop() ()
(gdb) up
#3  0x0000000000402f77 in quill::detail::LogManagerSingleton::~LogManagerSingleton() ()
(gdb) up
#4  0x0000000000524ab8 in __run_exit_handlers ()
(gdb) up
#5  0x0000000000524caa in exit ()
(gdb) up
#6  0x0000000000511bec in __libc_start_main ()
(gdb) up
#7  0x000000000041edea in _start ()
(gdb) up
Initial frame selected; you cannot go up.

@odygrd
Copy link
Owner

odygrd commented Mar 28, 2020

thanks, what linux distro are you using ? And how did you install clang 9.0 ? I would like to recreate the os image you are using to see if I can reproduce

@hungptit
Copy link
Author

hungptit commented Mar 28, 2020

I use Gentoo and I get segv using both gcc and clang++. Below is the detail information

~ $ gcc --version                                                                                                                                                                                                                     
gcc (Gentoo 9.3.0 p1) 9.3.0                                                                                                                                                                                                           
Copyright (C) 2019 Free Software Foundation, Inc.                                                                                                                                                                                     
This is free software; see the source for copying conditions.  There is NO                                                                                                                                                            
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                                                                                                                                                           
                                                                                                                                                                                                                                      
~ $ clang++ --version                                                                                                                                                                                                                 
clang version 9.0.1                                                                                                                                                                                                                   
Target: x86_64-pc-linux-gnu                                                                                                                                                                                                           
Thread model: posix                                                                                                                                                                                                                   
InstalledDir: /usr/lib/llvm/9/bin        

I take another quick look at the problem and I find out that -static flag is the main problem. Can you check if test_quill does not depend on any library when compiling with -static flag in your Ubuntu machine?

Compiled without -static flag

~/working/cppidioms/src $ ldd ./test_quill                                                                                                                                                                                            
        linux-vdso.so.1 (0x00007fff719be000)                                                                                                                                                                                          
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2b2e26f000)                                                                                                                                                                
        libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/libstdc++.so.6 (0x00007f2b2dff1000)                                                                                                                                  
        libm.so.6 => /lib64/libm.so.6 (0x00007f2b2deb7000)                                                                                                                                                                            
        libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/libgcc_s.so.1 (0x00007f2b2de9d000)                                                                                                                                    
        libc.so.6 => /lib64/libc.so.6 (0x00007f2b2dcde000)                                                                                                                                                                            
        /lib64/ld-linux-x86-64.so.2 (0x00007f2b2e32e000)                                                                                                                                                                              

Compiled using -static flag

~/working/cppidioms/src $ ldd ./test_quill                                                                                                                                                                                            
        not a dynamic executable    

@odygrd
Copy link
Owner

odygrd commented Mar 29, 2020

Hello, thanks for looking into it.

I managed to reproduce the clang va_args error and the -static error

The library is using std::thread to spawn a backend logging thread and on linux std::thread depends on pthread which is usually linked dynamically.

Using gcc 9 or clang9 - The behaviour is similar :

  • I get the same segfault as you when using '-static'. This looks related to this. I was able to run without segfault using the compilation flags :
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive")

  • The example runs okay and those extra compilation flags are not needed when i remove -static

Can you try using the above compilation flags and let me know if it works ?

Also does it run okay for you without segfault with clang9 when you remove -static -pedantic -pedantic-errors ?

Also, the library sets it's own compiler flags for the library target only in cmake

If you want to compile the example and still use -static for the example not the library I would recommend using something like this rather than passing the arguments globally :

add_executable(example_trivial example_trivial.cpp)
target_link_libraries(example_trivial quill)
target_compile_options(example_trivial PRIVATE -static)

This would compile only the target example_trivial with -static but still quill library will be built without -static

@hungptit
Copy link
Author

hungptit commented Apr 1, 2020

I think my problem will go away if quill can be used in header only build. Do you plan to support header only build?

@odygrd
Copy link
Owner

odygrd commented Apr 3, 2020

as mentioned here it will require many changes for this.

can you try adding -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive to your compilation flags and let me know if it goes away ?

@hungptit
Copy link
Author

hungptit commented Apr 4, 2020

I confirm that the problem is go away if I use above linking options, however, the output of ldd shows that the final binary do depend on my system dynamic libraries. I will try quill again when the header only build is supported.

Thank a lot for your help.
Hung

 ~/working/cppidioms/src $ ldd ./test_quill
        linux-vdso.so.1 (0x00007ffcd7ff9000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f34b5869000)
        libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/libstdc++.so.6 (0x00007f34b55eb000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f34b54b1000)
        libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/libgcc_s.so.1 (0x00007f34b5497000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f34b52d8000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f34b58cd000). 

@hungptit hungptit closed this as completed Apr 4, 2020
@hungptit
Copy link
Author

hungptit commented Apr 4, 2020

BTW, I really like quill and I look forward to try it out in the near future. My use cases is quite special because my binaries need to run on different Linux machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants