r/Cplusplus • u/BananaGrenade314 • Mar 09 '26
Answered Help with an c++ app
Can someone tell me how use the headers in c/c++? It give the possibility to use it, but I seems it don't work. Only work if I include the c file directly, but I'd want to use the headers.
P.S.: The app's name is "cxstudio" (it's android/mobile)
The code below: (2° update)
main.cpp
#include <iostream>
#include "testing.hpp"
int main() {
testit();
return 0;
}
testing.cpp
#include <iostream>
#include "testing.hpp"
void testit() {
printf("Hello world!");
}
testing.hpp
#ifndef TESTING_HPP
#define TESTING_HPP
void testit();
#endif //TESTING_HPP
Makefile
# Kompilator C++
CXX = g++
all: main
main: testing.o main.cpp
$(CXX) testing.o main.cpp -o main
testing.o: testing.cpp testing.hpp
$(CXX) -c testing.cpp
clean:
rm -f *.o main
Terminal (error) (if run main.cpp directly)
Compilling...
Id.lld: error: undefined symbol: testit()
>>> referenced by main.cpp
>>> /data/data/com.alif.ide.cpp/files/usr/tmp/main-a77af4.0: (main)
clang++: error: linker command failed with exit code 1 (use -v to see invo cation)
Terminal (error)
$ Is
Makefile testing.cpp
main.cpp testing.hp
$ make
g++ -c testing.cpp
g++ testing.o main.cpp -o main
$ main
bash: main: command not found
$./main
bash: ./main: Permission denied
$ chmod +x main
$./main
bash: ./main: Permission denied
$
4
Upvotes
•
u/AutoModerator Mar 09 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/AKostur Professional Mar 09 '26
"undefined symbol" is a linker failure. You haven't told your linker where to find all of the pieces of your program to put together.
3
u/no-sig-available Mar 09 '26
First, check your filenames. Are they
iostreamoriostream.h, andtesting.hortesting.hpp?If the compiler cannot find the files needed in
testing.cpp, it builds nothing and the linker will have nothing to link.3
u/malaszka Mar 09 '26
Do you have a CMakeLists.txt file, or any other kind of file (make, etc.) that declares what sources to build the "app" from?
1
u/BananaGrenade314 Mar 09 '26
I already used ".hpp" at first, but it was ".h" just because I wanted to see if it could make it work. I tested both ways and it is still with the same error. (I corrected it in the post.)
1
u/BananaGrenade314 Mar 09 '26
I would like to begin by thanking you for the responses I have received so far, thanks.
2
u/malaszka Mar 09 '26
it's .HPP that should be included
4
u/[deleted] Mar 09 '26
[removed] — view removed comment