0

I'm trying to wrap elements of this ascii-art repo. I'm using g++ 9.3.0 cpp-17 on ubuntu 20.04. Here's a Tree overview of the dirs -

├── build
├── in
│   ├── js.bmp
│   ├── socrates.bmp
│   └── woman.bmp
├── LICENSE
├── makefile
├── out
│   ├── js.txt
│   ├── socrates.txt
│   └── woman.txt
├── README.md
├── results
│   ├── js.png
│   ├── socrates.png
│   └── woman.png
└── src
    ├── headers
    │   ├── bmp_body.h
    │   ├── bmp_header.h
    │   ├── bmp_image.h
    │   ├── bmp_process.h
    │   └── bmp_reader.h
    ├── lib
    │   ├── bmp_body.cpp
    │   ├── bmp_header.cpp
    │   ├── bmp_image.cpp
    │   ├── bmp_process.cpp
    │   └── bmp_reader.cpp
    └── main.cpp

Following my intuition, compiling solely src/main.cpp, namely g++ -Wall src/main.cpp results in undefined reference errors.

/usr/bin/ld: /tmp/ccTouLyj.o: in function `main':
main.cpp:(.text+0x12f): undefined reference to `new_bmp_image()'
/usr/bin/ld: main.cpp:(.text+0x162): undefined reference to `bmp_read_header(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char*)'
/usr/bin/ld: main.cpp:(.text+0x1aa): undefined reference to `delete_bmp_image(bmp_image*)'
/usr/bin/ld: main.cpp:(.text+0x262): undefined reference to `bmp_read_body(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, int, int, int)'
/usr/bin/ld: main.cpp:(.text+0x288): undefined reference to `bmp_to_ascii(bmp_image*)'
/usr/bin/ld: main.cpp:(.text+0x297): undefined reference to `delete_bmp_image(bmp_image*)'
collect2: error: ld returned 1 exit status

compiling every file under src, namely main.cpp and lib/*.cpp, results in success.

Namely the command, g++ -Wall src/main.cpp src/lib/*.cpp results a successful complilation.

This might be obvious for experienced c++ programmers, but I'm confused as to what's Really going on. I want to understand why and why not, it did what it did. Although I can barely intuitively understand what's going on, a little more elaboration on this would be awesome :)

PatXio
  • 129
  • 2
  • 16
  • Nothing really more to say than "this is how C++ works". What exactly is unclear to you about "if you don't compile code that's required by the entire application then it doesn't work"? – Sam Varshavchik Dec 31 '20 at 17:25
  • 1
    @PatXio It is perfectly valid to compile a source file with undefined references so long as you don't try to link it into an executable. You can do this with the -c flag using g++. For more information, research the steps of building an executable from c++ source files (e.g. preprocessing, compilation, and linking). – Alexander Guyer Dec 31 '20 at 17:32
  • oh ok. I see. Thanks, @Nerdizzle for the elaboration. But when you say it is perfectly valid to compile with undefined refs, the process ends with -1, so that means the executable can be compiled to (at least that's what with my case) – PatXio Dec 31 '20 at 17:40
  • I'm not sure what you mean. What process ends with -1? The compilation process (i.e. g++)? – Alexander Guyer Jan 04 '21 at 20:39
  • Yes @Nerdizzle! – PatXio Jan 05 '21 at 04:08
  • There must be something wrong with your code. If you're unsure why, feel free to come up with a small reproducible example and open another question. – Alexander Guyer Jan 05 '21 at 16:25

0 Answers0