FreeBSD 中的 Helloworld C++ 生成文件

Helloworld c++ makefile in FreeBSD

尝试使用 PMAKE(默认 FreeBSD make)编译我的第一个 c++ 程序。

helloworld.cpp:

#include <iostream>

int main() {

        std::cout << "Hello World!" << std::endl;
        std::cin.get();
}

生成文件:

OBJS    = helloworld.o
CFLAGS  = -Wall -Werror -std=c++14

helloworld      : $(OBJS)
        cc $(.ALLSRC) -o $(.TARGET)

helloworld.o    : helloworld.cpp
        cc -c helloworld.cpp

输出

$make
cc helloworld.o -o helloworld
ld: error: undefined symbol: std::__1::cout
>>> referenced by helloworld.cpp
>>>               helloworld.o:(main)

ld: error: undefined symbol: std::__1::cin
>>> referenced by helloworld.cpp
>>>               helloworld.o:(main)

ld: error: undefined symbol: std::__1::basic_istream<char, std::__1::char_traits<char> >::get()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(main)

ld: error: undefined symbol: std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&))

ld: error: undefined symbol: std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&))

ld: error: undefined symbol: std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))

ld: error: undefined symbol: std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))

ld: error: undefined symbol: __cxa_begin_catch
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))
>>> referenced by helloworld.cpp
>>>               helloworld.o:(__clang_call_terminate)

ld: error: undefined symbol: std::__1::ios_base::__set_badbit_and_consider_rethrow()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))

ld: error: undefined symbol: __cxa_end_catch
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long))

ld: error: undefined symbol: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char))
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char))

ld: error: undefined symbol: std::terminate()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(__clang_call_terminate)

ld: error: undefined symbol: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)
>>> referenced by helloworld.cpp
ld: error: undefined symbol: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(unsigned long, char))

ld: error: undefined symbol: std::__1::ios_base::getloc() const
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const)

ld: error: undefined symbol: std::__1::locale::~locale()
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const)
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const)

ld: error: undefined symbol: std::__1::ctype<char>::id
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&))

ld: error: undefined symbol: std::__1::locale::use_facet(std::__1::locale::id&) const
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&))

ld: error: undefined symbol: std::__1::ios_base::clear(unsigned int)
>>> referenced by helloworld.cpp
>>>               helloworld.o:(std::__1::ios_base::setstate(unsigned int))

ld: error: undefined symbol: __gxx_personality_v0
>>> referenced by helloworld.cpp
>>>               helloworld.o:(.eh_frame+0x4AB)
cc: error: linker command failed with exit code 1 (use -v to see invocation

我正在使用 FreeBSD 12.2,默认为 clang 10.0.1

注意:cc -o helloworld helloworld.cpp也不行。

这个问题可能很基础,但我还是卡住了。

我建议只使用 CMake 所以只需创建一个名为 CMakeLists.txt 的文件,内容为

cmake_minimum_required(VERSION 3.1)
project(helloworld)

add_executable(${PROJECT_NAME} helloworld.cpp )
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)

然后就可以在每个 OS 上编译您的代码,包括 BSD 和

mkdir bin
cd bin
cmake ..
cmake -build .

C 和 C++ 是两种不同的语言。使用 C++ 编译器编译 C++。在您的情况下,C++ 编译器可能称为 c++,而不是 cc