为什么clang ++编译代码而clang -x c ++失败?使用std :: string进行ncurses

时间:2017-01-14 09:24:48

标签: c++ clang clang++

我有一个看起来很奇怪的问题。我的代码是:

#include <string>
#include <chrono>
#include <thread>
#include <ncurses.h>
int main(int argc, char const *argv[])
{
    const unsigned int refresh_time_miliseconds = 500;
    const int exit_key1 = 'q';
    const int exit_key2 = 'Q';

    initscr();                     // Start curses mode
    cbreak();                      
    nodelay(stdscr, TRUE);         // getch is noblocking
    noecho();                      // don't echo keyboard input
    curs_set(0);                   // make cursor invisible
    printw("To quit hit the key '%c' or '%c'\n", exit_key1, exit_key2);
    refresh();                     // Print it on the screen

    int row, col;
    getyx(stdscr,row,col);
    int counter = 0;
    int ch = 0;
    while(ch != exit_key1 || ch != exit_key1) {
        mvprintw(row, col, "Counter: %d\n", counter++);
        std::string data = "Example";
        printw("Data vector: %s\n", data.c_str());
        refresh();
        ch = getch();
        std::this_thread::sleep_for(std::chrono::milliseconds(
        refresh_time_miliseconds));
    }

    endwin();                      // End curses mode
    return 0;   
}

它只是启动ncurses并打印计数器值和示例文本,直到用户点击&#39; q&#39;或者&#39; Q&#39;键。

我试图用以下方法编译它:
clang++ -o monitor -std=c++11 -Wall monitor.cpp --verbose -lncurses 它完美无缺,但使用:
clang -x c++ -o monitor -std=c++11 -Wall monitor.cpp --verbose -lncurses 以下输出失败:

clang version 3.8.1-16 (tags/RELEASE_381/final)  
Target: x86_64-pc-linux-gnu  
Thread model: posix  
InstalledDir: /usr/bin  
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9  
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3  
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1  
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1  
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9  
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3  
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1  
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.1  
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1  
Candidate multilib: .;@m64  
Selected multilib: .;@m64  
 "/usr/lib/llvm-3.8/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name monitor.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/c++/6.2.1 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/x86_64-linux-gnu/c++/6.2.1 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/x86_64-linux-gnu/c++/6.2.1 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/c++/6.2.1/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/mike/REPOS/AGH_SO_Coffee/PROBLEM -ferror-limit 19 -fmessage-length 158 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/monitor-9488c0.o -x c++ monitor.cpp  
clang -cc1 version 3.8.1 based upon LLVM 3.8.1 default target x86_64-pc-linux-gnu  
ignoring nonexistent directory "/include"  
ignoring duplicate directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/x86_64-linux-gnu/c++/6.2.1"  
#include "..." search starts here:  
#include <...> search starts here:  
 /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/c++/6.2.1  
 /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/x86_64-linux-gnu/c++/6.2.1  
 /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../../include/c++/6.2.1/backward  
 /usr/local/include  
 /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include  
 /usr/include/x86_64-linux-gnu  
 /usr/include  
End of search list.  
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o monitor /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../.. -L/usr/lib/llvm-3.8/bin/../lib -L/lib -L/usr/lib /tmp/monitor-9488c0.o -lncurses -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.1/../../../x86_64-linux-gnu/crtn.o  
/tmp/monitor-9488c0.o: In function `main':  
monitor.cpp:(.text+0x190): undefined reference to `std::allocator<char>::allocator()'  
monitor.cpp:(.text+0x1a7): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'  
monitor.cpp:(.text+0x1b5): undefined reference to `std::allocator<char>::~allocator()'  
monitor.cpp:(.text+0x1be): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'  
monitor.cpp:(.text+0x237): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'  
monitor.cpp:(.text+0x24e): undefined reference to `std::allocator<char>::~allocator()'  
monitor.cpp:(.text+0x265): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'  
/tmp/monitor-9488c0.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'  
clang: error: linker command failed with exit code 1 (use -v to see invocation)  

我注意到如果我注释掉所有对std :: string的引用:

std::string data = "Example";
printw("Data vector: %s\n", data.c_str());

然后两个编译命令都成功。

你有什么想法我为什么观察这种行为?

1 个答案:

答案 0 :(得分:0)

  

患者:&#34;当我这样做时,我的手臂疼!&#34;

     医生:&#34;不要这样做!&#34;

clang是一个c编译器。它没有自动链接c ++运行时库(为什么会这样?)

clang ++是一个c ++编译器。