Using libXml2 in C++ with Netbeans - c++

I'm having a problem getting C++ code to compile properly in Netbeans, specifically code that deals with libXml2. I downloaded libXml2, put it in the include folder and I know that the code itself compiles fine without any libxml2 references in it, however, when I add the following functions:
void XmlParser::processNode(xmlTextReaderPtr reader){
const xmlChar *name, *value;
name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";
value = xmlTextReaderConstValue(reader);
printf("%d %d %s %d %d",
xmlTextReaderDepth(reader),
xmlTextReaderNodeType(reader),
name,
xmlTextReaderIsEmptyElement(reader),
xmlTextReaderHasValue(reader));
if (value == NULL)
printf("\n");
else {
if (xmlStrlen(value) > 40)
printf(" %.40s...\n", value);
else
printf(" %s\n", value);
}
}
void XmlParser::streamFile(const char *filename) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
//processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
}
I get the following result when I click build:
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser11processNodeEP14_xmlTextReader':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:26: undefined reference toxmlTextReaderConstName'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:30: undefined reference to xmlTextReaderConstValue'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference toxmlTextReaderHasValue'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderIsEmptyElement'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference toxmlTextReaderNodeType'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderDepth'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:41: undefined reference toxmlStrlen'
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser10streamFileEPKc':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:51: undefined reference toxmlReaderForFile'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:53: undefined reference to xmlTextReaderRead'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:56: undefined reference toxmlTextReaderRead'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:58: undefined reference to `xmlFreeTextReader'
Which suggests that I have some sort of an issue compiling the program using libXml2. I saw that someone right here had the same error, and that the answer to this problem is correctly setting up the argument for compiling the program, however, I cannot figure out how to do this in NetBeans/Windows. I think what I need is details on how to actually get the arguments right. Any help would be appreciated. Thank you.

Add -lxml2 (gcc) or libxml2.lib (visual studio) to your linker.

Related

OpenSSL: Initializing EVP_PKEY gives symbol error

I have a function that takes as parameter an unique_ptr of cctx and EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY mode. When I call these two init_pkey_ctx functions from constructor, I get following symbol error.
symbol lookup error: libkerf.code.so: undefined symbol: EVP_PKEY_CTX_set_hkdf_mode
I have the following piece of code:
NCF_SYMBOL_VISIBLE kerf_decryptor():
cctx(EVP_CIPHER_CTX_new(), &EVP_CIPHER_CTX_free),
pctx_extract(EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr), &EVP_PKEY_CTX_free),
pctx_expand(EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr), &EVP_PKEY_CTX_free)
{
if (!cctx || !pctx_expand || !pctx_extract)
NCF_LOG_ERROR(acu_event_id::sei_cl_mod_code_classify_kerf_fail_decryption, "Initializing the kerf_decryptor: EVP_CIPHER_CTX_new");
init_pkey_ctx(pctx_extract.get(),EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY);
init_pkey_ctx(pctx_expand.get(), EVP_PKEY_HKDEF_MODE_EXPAND_ONLY);
}
//initialize pkey(the source of error)
void init_pkey_ctx(EVP_PKEY_CTX *ctx, std::int32_t mode)
{
if (EVP_PKEY_derive_init(ctx) <= 0) {
NCF_LOG_ERROR(acu_event_id::sei_cl_mod_code_classify_kerf_fail_decryption, "EVP_PKEY_derive_init");
}
if (EVP_PKEY_CTX_set_hkdf_mode(ctx, mode) <= 0){
NCF_LOG_ERROR(acu_event_id::sei_cl_mod_code_classify_kerf_fail_decryption, "EVP_PKEY_CTX_set_hkdf_mode");
}
if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) <= 0){
NCF_LOG_ERROR(acu_event_id::sei_cl_mod_code_classify_kerf_fail_decryption, "EVP_PKEY_CTX_set_hkdf_md");
}
}
std::unique_ptr<EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)> cctx;
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> pctx_extract;
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> pctx_expand;

Locating functions with IDIA SDK - strange results

I am trying to locate a specific function in a .pdb file. Originally I compiled a simple "hello, world" program, and analyzed the name of the functions using the IDiaSymbol::get_name method, but I couldn't locate my function.
After this, I tried including a __declspec(naked) void myFunction(void){} function in my helloworld.pdb file, in order to use the IDiaSymbol::get_isNaked method to locate my function, however, when I did this, nothing at all was printed - entailing there are no naked functions in my code.
//After initialization, creating instance, etc
IDiaEnumSymbols* pUnknown = NULL;
if (pTable->QueryInterface(__uuidof(IDiaEnumSymbols), (void**) &pUnknown) == S_OK) {
printf("Supports Symbol module\n");
CComPtr<IDiaSymbol> pSymbol;
int counter = 0;
for (LONG i = 0; i < blongTableCount; i++)
{
if (pUnknown->Item(i, &pSymbol) != S_OK) {
fprintf(stderr, "Error: pUnknown->Item");
}
BOOL isFunction;
if (pSymbol->get_function(&isFunction) == S_OK) {
if (isFunction == TRUE) {
counter += 1;
printf("Number of functions: %d", counter);
//With the following I could not find my functions
BSTR symName;
if (pSymbol->get_name(&symName) == S_OK) {
printf("Name of symbol: %S\n", symName);
}
//Check for naked functions - I included a declspec(naked) function for testing.
BOOL pFlag;
if (pSymbol->get_isNaked(&pFlag) == S_OK) {
printf("This is a naked function");
}
}
}
pSymbol = NULL;
}
}
EDIT: Included my simple .pdb program below (was a "hello world program", now contains a simple __declspec(naked) function):
#include <iostream>
__declspec(naked) void myFunction(void) {
__asm {
ret
}
}
int main()
{
myFunction();
return 0;
}
What I expected from parsing the symbol table: The same results you would get when parsing an ELF file on *NIX - a symbol table containing the actual names I wrote for my function, so something like ".text myFunction"
What is actually printed out: Many Winapi functions and other assembler created functions, probably due to optimizing out of my function.
Example:
Name of symbol: main Name of symbol: __acrt_thread_attach
Name of symbol: _RTC_NumErrors Name of symbol: ReadNoFence64
Name of symbol: __setusermatherr Name of symbol:
_RTC_SetErrorFuncW Name of symbol: IsProcessorFeaturePresent Name of symbol: GetLastError Name of symbol: __acrt_initialize

C/C++ Threading in Linux (Raspbian) using VS2019 on Windows 10 -pthread - Can't compile

I'm trying to do my first bit of threading but no matter what I've tried I can't get this to compile.
I've gone back to trying to compile some demo code and I'm getting the same problem as in my program.
If I run a simple print hello world it compiles and deploys the program fine and I can simply navigate to and run it directly on the Pi4.
Threading demo code
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void* arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if (pthread_equal(id, tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for (i = 0; i < (0xFFFFFFFF); i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while (i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
When I compile I get
Error /home/pi/projects/cpp_raspbian_thread_101/obj/x64/Debug/main.o: in function `main':
Error undefined reference to `pthread_create'
Error ld returned 1 exit status
To resolve this I've tried to add -pthread or -lpthread to
Project > Properties > Configuration Properties > C/C++ > Command Line > Addiitional Options
That does nothing, I'm not really sure if this is the correct place to put this.
I'm building in VS2019 so I'm not building from the command line, I don't know where to add this argument.
I have also tried installing pthreads in NuGet but that doesn't help.
Other software like VSCode seem to have files that could add this to but I'm lost in VS2019
Any help is appreciated.
EDIT:
Thanks for responses
OK so as #Eljay suggested I'm trying to use std::thread (again) but have the same problem.
// thread example
#include <iostream>
#include <thread>
void foo()
{
// do stuff...
}
int main()
{
std::thread first(foo);
return 0;
}
Log file
Validating sources
Copying sources remotely to '10.0.0.2'
Validating architecture
Validating architecture
Starting remote build
Compiling sources:
main.cpp
Linking objects
/usr/bin/ld : error : /home/pi/projects/cpp_raspbian_thread_101/obj/ARM/Debug/main.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
/usr/include/c++/8/thread(135): error : undefined reference to `pthread_create'
collect2 : error : ld returned 1 exit status
So I'm back to the pthread_create problem again
OK both code examples now compile and run.
As I originally thought, I needed to add -pthread somewhere in VS2019 and I was putting it in the wrong section.
Go to
Project Properties > Configuration Properties > Linker > Command Line
Add -pthread to Additional Options box and Apply.
I hope that saves someone else the 3 days it took me to sort it!

Undefined reference to 'getcwd' and 'mkdir' when building Google test project

even if I have been strictly following the tutorials from this post, I can't get my google test demo program to compile.
I'm using Eclipse on Windows 10 x64, and the ARM GCC embedded toolchain to compile my code, since I will eventually need to run unit tests on embedded devices.
My problem is that when I try to Build the project I get those errors :
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::GetCurrentDir()':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9598: undefined reference to `getcwd'
c:/program files (x86)/gnu tools arm embedded/9 2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: ./contrib/gtest/gtest-all.o: in function `testing::internal::FilePath::CreateFolder() const':
C:\Users\Hugo\eclipse\eclipse-workspace\test_gtest\Debug/../contrib/gtest/gtest-all.cc:9823: undefined reference to `mkdir'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:61: test_gtest.elf] Error 1
It comes more precisely from those lines of code in the gtest_all.cc file:
For the undefined reference to 'getcwd'
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32
// These platforms do not have a current directory, so we just return
// something reasonable.
return FilePath(kCurrentDirectoryString);
#elif GTEST_OS_WINDOWS
char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
#else
char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
char* result = getcwd(cwd, sizeof(cwd));
# if GTEST_OS_NACL
// getcwd will likely fail in NaCl due to the sandbox, so return something
// reasonable. The user may have provided a shim implementation for getcwd,
// however, so fallback only when failure is detected.
return FilePath(result == nullptr ? kCurrentDirectoryString : cwd);
# endif // GTEST_OS_NACL
return FilePath(result == nullptr ? "" : cwd);
#endif // GTEST_OS_WINDOWS_MOBILE
}
For the undefined reference to 'mkdir' :
bool FilePath::CreateFolder() const {
#if GTEST_OS_WINDOWS_MOBILE
FilePath removed_sep(this->RemoveTrailingPathSeparator());
LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
int result = CreateDirectory(unicode, nullptr) ? 0 : -1;
delete [] unicode;
#elif GTEST_OS_WINDOWS
int result = _mkdir(pathname_.c_str());
#elif GTEST_OS_ESP8266
// do nothing
int result = 0;
#else
int result = mkdir(pathname_.c_str(), 0777);
#endif // GTEST_OS_WINDOWS_MOBILE
if (result == -1) {
return this->DirectoryExists(); // An error is OK if the directory exists.
}
return true; // No error.
}
I have checked that unistd.h was included. I've been searching a lot but can't seem to find any similar error as mine. The closest I could find has been solved by people using CMake to compile it, but I'm not using CMake at all here.
AFAIK, getcwd() and mkdir() is platform dependent.
It seem there have been similar issue with other library:
https://github.com/purduesigbots/pros/issues/176
As above link, you can try to define stub for missing symbols.
In my working platform, getcwd() and mkdir() even get removed from the header.
In such case, you can edit gtest directly, for example:
FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_CUSTOM_PLATFORM
return kCurrentDirectoryString;
...

Linking issues using OpenSSL in Ubuntu

I have installed OpenSSL using sudo apt-get install openssl-dev. When I try to compile it using Netbeans it gives following errors. How can I fix this problem?
g++ -lssl -o dist/Debug/GNU-Linux-x86/cppapplication_2 build/Debug/GNU-Linux-x86/main.o -L/home/sercan/Desktop/openssl-0.9.8h-1-lib/lib
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:46: undefined reference to `OPENSSL_add_all_algorithms_noconf'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:48: undefined reference to `ERR_load_crypto_strings'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:58: undefined reference to `d2i_PKCS12_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:66: undefined reference to `ERR_print_errors_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:72: undefined reference to `PKCS12_parse'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:76: undefined reference to `ERR_print_errors_fp'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:82: undefined reference to `PKCS12_free'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:96: undefined reference to `PEM_write_PrivateKey'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:104: undefined reference to `PEM_write_X509_AUX'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:108: undefined reference to `sk_num'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `sk_value'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:114: undefined reference to `PEM_write_X509_AUX'
/home/sercan/NetBeansProjects/CppApplication_2/main.cpp:112: undefined reference to `sk_num'
My code is here:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>
/* Simple PKCS#12 file reader */
int main(int argc, char **argv)
{
FILE *fp;
EVP_PKEY *pkey;
X509 *cert;
STACK_OF(X509) *ca = NULL;
PKCS12 *p12;
int i;
if (argc != 4) {
fprintf(stderr, "Usage: pkread p12file password opfile\n");
exit(1);
}
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
if (!(fp = fopen(argv[1], "rb"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp);
if (!p12) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit(1);
}
if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit(1);
}
PKCS12_free(p12);
if (!(fp = fopen(argv[3], "w"))) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1);
}
if (pkey) {
fprintf(fp, "***Private Key***\n");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
}
if (cert) {
fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUX(fp, cert);
}
if (ca && sk_X509_num(ca)) {
fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
}
fclose(fp);
return 0;
}
you need to link to libcrypto as well - add -lcrypto to the link line and your code should link correctly.
natsu:~/openssl% gcc -o test test.c -L/usr/lib -lssl -lcrypto
links correctly, while:
natsu:~/openssl% gcc -o test test.c -L/usr/lib -lssl
/tmp/ccvA7iNe.o: In function `main':
test.c:(.text+0x4c): undefined reference to `OPENSSL_add_all_algorithms_noconf'
test.c:(.text+0x51): undefined reference to `ERR_load_crypto_strings'
test.c:(.text+0xb9): undefined reference to `d2i_PKCS12_fp'
test.c:(.text+0x103): undefined reference to `ERR_print_errors_fp'
test.c:(.text+0x133): undefined reference to `PKCS12_parse'
test.c:(.text+0x16a): undefined reference to `ERR_print_errors_fp'
test.c:(.text+0x180): undefined reference to `PKCS12_free'
test.c:(.text+0x22c): undefined reference to `PEM_write_PrivateKey'
test.c:(.text+0x266): undefined reference to `PEM_write_X509_AUX'
test.c:(.text+0x27b): undefined reference to `sk_num'
test.c:(.text+0x2b7): undefined reference to `sk_value'
test.c:(.text+0x2c9): undefined reference to `PEM_write_X509_AUX'
test.c:(.text+0x2d9): undefined reference to `sk_num'
collect2: ld returned 1 exit status
does not.