Strange C++ compilation error with header file due to makefile setting? - c++

I have the following defined in the header file:
some function ...
if (table2[i] != ""){
new_table2[hash2(table2[i])] = table2[i];
table2[i] = "";
}
// std::cout << " first i : " << i << "\n";
}
for (int i=0; i < 150; ++i){
if (new_table1[i] != "" && !try_insert(new_table1[i])){
std::cout << " bad rehash : " << i << " " << new_table1[i] << "\n";
rehash();
}
}
Makefile looks like this:
CC=g++
CFLAGS=-Wall -Wextra -O3.
DEPS = somehash.h hashfunctions.h
OBJ = main.o
%.o: %.c $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
main: $(OBJ)
g++ -o $# $^ $(CFLAGS)
.PHONY: clean
clean:
rm -f *.o main
After I do make clean, make, and ./main, "first i" is still printed even though it is commented out. When I comment out " bad rehash ", then both of them are not printed. It seems the compiler did some auto optimization that causes this to be cached? How can I fix it?
Edit: I opened the file in another editor (was using Sublime), and the problem went away. It seem the problem is sublime.

Related

GCOV/LCOV ignores `used functions` declared before an `unused function`

The question may seem weird.
I tried to use GCOV/LCOV for my small project and practiced with simple code before applying it. While practicing it, I encountered an error that I had no idea how to solve.
The report created by LCOV showed that the functions declared before the unused function inside the source code file are reported as unused functions. In contrast, the functions displayed correct outputs when the binary file was executed.
The followings are actual codes used for the practice.
# makefile
CC = g++
CFLAG = -g -fPIC -fprofile-arcs -ftest-coverage
RM = rm -rf
main.o : main.cpp
$(CC) $(CFLAG) -c -Wall -Werror main.cpp
dummy_class.o : dummy_class.cpp
$(CC) $(CFLAG) -c -Wall -Werror dummy_class.cpp
build : main.o dummy_class.o
$(CC) $(CFLAG) -o main main.o dummy_class.o
gcov: main.cpp dummy_class.cpp
gcov main.cpp dummy_class.cpp
coverage.info: gcov
lcov --capture --directory . --output-file coverage.info
lcov -remove coverage.info "/usr/include/*" "/usr/local/include/*" --output-file coverage.info
report : coverage.info
genhtml coverage.info --output-directory ./out
clean :
rm -f main
rm -f *.o *.so *.gcno *.gcda *.gcov coverage.info
rm -r out
do :
make build
./main
make report
// dummy_class.hpp
#pragma once
void func_even_case(void);
void func_odd_case(void);
void func_not_reachable(void);
void dummy(void);
void dummy2(void);
void dummy3(void);
// dummy_class.cpp
#include <iostream>
#include <vector>
#include "dummy_class.hpp"
void func_even_case(void)
{
std::cout << "This is even case" << std::endl;
}
void func_odd_case(void)
{
std::cout << "This is odd case" << std::endl;
}
void func_not_reachable(void)
{
std::cout << "This is not reachable" << std::endl;
}
void dummy(void)
{
std::cout << "This is dummy1." << std::endl;
}
void dummy2(void)
{
std::cout << "This is dummy2." << std::endl;
}
void dummy3(void)
{
std::cout << "This is dummy3." << std::endl;
}
// main.cpp
#include <iostream>
#include "dummy_class.hpp"
int main(void)
{
for (int i = 0; i < 10; ++i)
{
if (i % 2 == 0)
{
func_even_case();
}
else if (i % 2 != 0)
{
func_odd_case();
}
else
{
func_not_reachable();
}
}
func_not_reachable();
dummy();
dummy2();
dummy3();
return 0;
}
When func_not_reachable() is placed outside the for-loop, the report returns
Overall coverage rate:
lines......: 96.7% (29 of 30 lines)
functions..: 100.0% (7 of 7 functions)
and the result is expected.
When func_not_reachable() is removed, the expected result was
Overall coverage rate:
lines......: 86.5% (25 of 29 lines)
functions..: 100.0% (6 of 7 functions)
since func_not_reachable() is the one that will not be executed.
However, the actual result was
Overall coverage rate:
lines......: 65.5% (19 of 29 lines)
functions..: 57.1% (4 of 7 functions)
If the dummy_class.cpp is modified as following
#include <iostream>
#include <vector>
#include "dummy_class.hpp"
void func_even_case(void)
{
std::cout << "This is even case" << std::endl;
}
void func_odd_case(void)
{
std::cout << "This is odd case" << std::endl;
}
void dummy(void)
{
std::cout << "This is dummy1." << std::endl;
}
void dummy2(void)
{
std::cout << "This is dummy2." << std::endl;
}
void dummy3(void)
{
std::cout << "This is dummy3." << std::endl;
}
// unused function declared at the end of the source code.
void func_not_reachable(void)
{
std::cout << "This is not reachable" << std::endl;
}
The report result becomes follows.
Overall coverage rate:
lines......: 34.5% (10 of 29 lines)
functions..: 14.3% (1 of 7 functions)
I am sure I made errors while using GCOV and LCOV, but I cannot figure out where I made a mistake.
Can someone tell me where I made a mistake?
The above code was executed on the following.
Ubuntu 20.04.4 LTS
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
gcov (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
lcov: LCOV version 1.14
Find out the source of the problem.
The problem was related to the compiler (g++ 11.1.0) or gcov (9.3.0) I used.
The code coverage reported the correct result when the compiler changed from g++ to clang.
To ensure the problem is related to the specific version of the compiler (g++ 11.1.0), I upgraded the system from Ubuntu 20.04.4 LTS to Ubuntu 22.04.4 LTS.
The test condition became as follow
Ubuntu 22.04.4 LTS
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
gcov (Ubuntu 11.2.0-19ubuntu1) 11.2.0
After the update had been made, the code coverage reported the correct result with g++.

C++ - Compile and link multiple files

I have a project with the following structure:
Item.cpp
Item.h
main.cpp
Makefile
The following source code is in the Item.h file:
class Item {
public:
Item();
~Item();
};
The following source code is in the Item.cpp file:
#include <iostream>
#include "Item.h"
Item::Item() {
std::cout << "Item created..." << std::endl;
}
Item::~Item() {
std::cout << "Item destroyed..." << std::endl;
}
The following source code is the content of the main.cpp file:
#include "Item.h"
#include <iostream>
int main() {
std::cout << "Initialize program..." << std::endl;
Item item_1();
std::cout << "Hello world!" << std::endl;
return 0;
}
And finally, the following source code is the Makefile file:
CXX = g++
all: main item
$(CXX) -o sales.o main.o Item.o
main:
$(CXX) -c main.cpp
item:
$(CXX) -c Item.cpp
clean:
rm -rf *.o
When I run the make command and then I run the compiled code with the command ./sales.o, I get the following output:
Initialize program...
Hello world!
Why is the output of the constructor method of the class Item not printed in the console? I found in some web pages that you can compile the source codes in steps and then you can link it with the -o option when using g++ but it does not work in this case. How can I compile this source codes step by step and then link it in the Makefile?
I'm surely you ignored this warning :
warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
#include "Item.h"
#include <iostream>
int main() {
std::cout << "Initialize program..." << std::endl;
Item item_1;
std::cout << "Hello world!" << std::endl;
return 0;
}
just remove parentheses it will be work
test : https://godbolt.org/z/KrdrhvsrW

g++ complains about undefined reference even though library is included

I have a sample file like so, lets call it dnn_mmod_face_detection_ex.cpp
#include <iostream>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------------------
template <long num_filters, typename SUBNET> using con5d = con<num_filters,5,5,2,2,SUBNET>;
template <long num_filters, typename SUBNET> using con5 = con<num_filters,5,5,1,1,SUBNET>;
template <typename SUBNET> using downsampler = relu<affine<con5d<32, relu<affine<con5d<32, relu<affine<con5d<16,SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5 = relu<affine<con5<45,SUBNET>>>;
using net_type = loss_mmod<con<1,9,9,1,1,rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<6>>>>>>>>;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv) try
{
if (argc == 1)
{
cout << "Call this program like this:" << endl;
cout << "./dnn_mmod_face_detection_ex mmod_human_face_detector.dat faces/*.jpg" << endl;
cout << "\nYou can get the mmod_human_face_detector.dat file from:\n";
cout << "http://dlib.net/files/mmod_human_face_detector.dat.bz2" << endl;
return 0;
}
net_type net;
deserialize(argv[1]) >> net;
image_window win;
for (int i = 2; i < argc; ++i)
{
matrix<rgb_pixel> img;
load_image(img, argv[i]);
// Upsampling the image will allow us to detect smaller faces but will cause the
// program to use more RAM and run longer.
while(img.size() < 1800*1800)
pyramid_up(img);
// Note that you can process a bunch of images in a std::vector at once and it runs
// much faster, since this will form mini-batches of images and therefore get
// better parallelism out of your GPU hardware. However, all the images must be
// the same size. To avoid this requirement on images being the same size we
// process them individually in this example.
auto dets = net(img);
win.clear_overlay();
win.set_image(img);
for (auto&& d : dets)
win.add_overlay(d);
cout << "Hit enter to process the next image." << endl;
cin.get();
}
}
catch(std::exception& e)
{
cout << e.what() << endl;
}
I have a Makefile like so
CC=g++
CFLAGS=-c -Wall -std=c++11 -v
LDFLAGS=-L/usr/local/cuda/lib64 -ldlib -lcudnn -lpthread -ldl -lrt -lX11 -lcublas -lcudnn -lcurand -lcusolver -lstdc++ -lm -lgcc_s -lc -lxcb -lXau -lXdmcp
SOURCES=dnn_mmod_face_detection_ex.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=dnn_mmod_face_detection_ex
INCLUDE=
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $(INCLUDE) $< -o $#
But I get undefined references to almost every dlib library. ie
dnn_mmod_face_detection_ex.cpp:(.text+0x267): undefined reference to `dlib::image_window::image_window()'
dnn_mmod_face_detection_ex.cpp:(.text._ZNK4dlib8gpu_data4hostEv[_ZNK4dlib8gpu_data4hostEv]+0x14): undefined reference to `dlib::gpu_data::copy_to_host() const'
dnn_mmod_face_detection_ex.cpp:(.text._ZN4dlib16resizable_tensorC2Ev[_ZN4dlib16resizable_tensorC5Ev]+0x31): undefined reference to `dlib::cuda::tensor_descriptor::tensor_descriptor()'
I know the following
This same example is compiled and is runs inside the /dlib/build/test... directory (dlib-19.9)
Those LDFLAGS I obtained by issuing the command ldd /dlib/build/test.../dnn_mmod_face_detection_ex
What is the right way to figure out what libraries are missing? I tried tracing the Cmake file(s) that dlib provided but it is more complicated than a particle accelerator.
It was a simple mistake of misplacing the LDFLAGS. It works when placed at the end like so
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# $(LDFLAGS)

"No such file or directory" Error when doing simple fstream in c++ while using makefile

I get the error "No such file or directory" when I run this at the top of my main:
Edit- This is my complete .cpp file:
#include <iostream>
#include <fstream>
#include <sstream>
#include "CommunicationNetwork.h"
using namespace std;
int DisplayMenu();
int main(int argc, char* argv[])
{
ifstream file;
string data;
file.open("message.txt");
if(file.is_open()){
cout << "WORKS" << endl;
}else {
cerr << "Error: " << strerror(errno) << endl;
}
CommunicationNetwork network;
bool gbye = false;
while(gbye == false){
int use = DisplayMenu();
switch(use) {
case 1:
network.buildNetwork();
break;
case 2:
network.printNetwork();
break;
case 3:
network.transmitMsg("message.txt");
break;
case 4: cout << "Add" << endl;
break;
case 5:
cout << "Goodbye!" << endl;
gbye = true;
}
}
return 0;
}
I have used this method countless times to open txt files but this is the first time I am using a makefile with my program which I'm guessing is the cause of the problem.
Does "message.txt" need to appear in my makefile somewhere? Is so where? What if I want to pass the filename as a command line argument?
My makefile looks like:
PROG = com
CC = g++
OBJS = CommunicationNetwork.o Assignment.o
CPPFLAGS = -Wall -std=c++11
$(PROG) : $(OBJS)
$(CC) -o $(PROG) $(OBJS)
CommunicationNetwork.o : CommunicationNetwork.h
$(CC) $(CPPFLAGS) -c CommunicationNetwork.cpp
Assignment.o :
$(CC) $(CPPFLAGS) -c Assignment.cpp
clean:
$(RM) $(PROG) $(OBJS)
The .txt file is saved in the same folder as my other files. I will get the error regardless of what .txt file I try. Let me know if more information is needed. Thanks!
Edit:
Both the .cpp and the .txt file are in the folder Assignment
for message.txt: /Users/tannerquigley/Library/Mobile Documents/com~apple~CloudDocs/School/CSCI 2270/Assignment
for Assignment.cpp: /Users/tannerquigley/Library/Mobile Documents/com~apple~CloudDocs/School/CSCI 2270/Assignment
in Terminal: res1-131-132-dhcp:Assignment tannerquigley$ make after I run a make clean
And then I open the "com" executable file that appears in the Assignment folder
Do not run the Unix Executable File that appears in the Folder. Run the program from the same terminal window using the command ./com or whatever you (I) named the program in the Makefile.

mysql C++ will not compile with make but will if g++ arguments put on cmd line

So i'm just trying to compile this sample mysql C++ program. It will compile and run just fine if i put g++ -Wall -I/usr/include/cppconn -o mysql_test mysql_test.cpp -L/usr/lib -lmysqlcppconn on the command line. If i try to use make, it fails due to a undefined function call. What is wrong with the make script?
g++ -o prog mysql_test.o
mysql_test.o: In function main':
mysql_test.cpp:(.text+0x3a): undefined reference toget_driver_instance'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Here is the make file:
CXX=g++
#CXXFLAGS=-g -std=c++0x -Wall -pedantic -lmysqlcppconn -lmysqlcppconn-static
CXXFLAGS=-g -std=c++0x -Wall -I/usr/include/cppconn -L/usr/lib -lmysqlcppconn
BIN=prog
SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)
all: $(OBJ)
$(CXX) -o $(BIN) $^
%.o: %.c
$(CXX) $# -c $<
clean:
rm -f *.o
rm $(BIN)
Here is the cpp source:
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
/* Connect to the MySQL test database */
con->setSchema("mysql");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
Thanks for any help with this.
Here is the working make:
CXX=g++
#CXXFLAGS=-g -std=c++0x -Wall -pedantic -lmysqlcppconn -lmysqlcppconn-static
CXXFLAGS=-g -std=c++0x -Wall -I/usr/include/cppconn
LDFLAGS=-L/usr/lib -lmysqlcppconn
BIN=prog
SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)
all: $(OBJ)
$(CXX) -o $(BIN) $(LDFLAGS) $^
%.o: %.c
$(CXX) $# -c $<
clean:
rm -f *.o
rm $(BIN)
-L/usr/lib -lmysqlcppconn needs to be in LD_FLAGS.